Getting Preview Crashed with the Red X

I am getting Preview Crash. If I remove line 30 below. then the Preview Crash does not occur. I have Restarted XCode, I have done a Project Clean, Then did a project rebuild.

I have also Created a new Project from scratch Named the project Debug, Add one new View File named MaskBootCamp once I select MaskBootCamp The Preview willl Crash with an Circle with a Red X in the center With Preview Crashed.

If I remove line 22 all is resolved and no exception occurs

Is this a Swift Bug or some error in the coding? Excepition Report listed below the code. report Below:

** MaskBootCamp .swift **


import SwiftUI

struct MaskBootCamp: View { @State var rating: Int = 0

var body: some View {
    
    ZStack{
        starsView
            .overlay(overlayView.mask(starsView))
    }
}


private var overlayView: some View {
    GeometryReader { geometry in
        ZStack(alignment: .leading) {
            Rectangle()
                .foregroundColor(.yellow)
                .frame(width: CGFloat(rating) / 5 * geometry.size.width)
        }
        //allowsHitTesting(false)
    }
}


private var starsView: some View {
    HStack{
        ForEach(1..<6) {index in
            Image(systemName: "star.fill")
                .font(.largeTitle)
                .foregroundColor(.gray)
                .onTapGesture {
                    rating = index
                }
        }
    }
}

}

#Preview { MaskBootCamp() }

Answered by bxw0405 in 773728022

Tested with using Simulator and IPhone. It builds but throw error

The issue only occurs on the allowsHitTesting in the OverlayView I have it commented out currently

Accepted Answer

Tested with using Simulator and IPhone. It builds but throw error

I found the issue.

I was calling

ZStack(){ }allowsHitTesting(false)

when maybe I should have. been calling

ZStack() {
}.allowsHitTesting(false)

This seems like a bug to me. I believe that this should have been a compile error. I am new and learningSwiftUI developer and would like to better understand this issue Any Comments would be appreciated.

Thanks

Getting Preview Crashed with the Red X
 
 
Q