UIImpactFeedbackGenerator seems to break SwiftUI Previews in Xcode 12

Hi!

I created a custom modifier which looks like this:

Code Block
extension View {
    func onTapGestureWithFeedback(action: @escaping () -> Void) -> some View {
        let impact = UIImpactFeedbackGenerator(style: .medium)
        let defaults = UserDefaults.standard
        let reduceHaptics = defaults.object(forKey:"reduceHaptics") as! Bool
        return self.onTapGesture {
            if !reduceHaptics { impact.impactOccurred() }
            action()
        }
    }
}


For some reason, while I use this modifier on a view, like this
Code Block
struct TestView: View {
    var body: some View {
        Text("Hello World")
            .onTapGestureWithFeedback {
                print("test")
            }
    }
}


the SwiftUI Preview breaks. This is the error I'm getting in the diagnosis window: MessageSendingError: Connection interrupted: send message to agent. This used to work fine in Xcode 11.

Please find attached the crash log as well.



Answered by torgeadelin in 616839022
The issue was caused by this error Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/ctorge/Desktop/quote/Quote/Services/Utils/UI/Modifiers.swift, line 97. It would be nice to get this kind of errors in the SwiftUI Preview Diagnosis.
Accepted Answer
The issue was caused by this error Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/ctorge/Desktop/quote/Quote/Services/Utils/UI/Modifiers.swift, line 97. It would be nice to get this kind of errors in the SwiftUI Preview Diagnosis.
I got this same error but for me it was because I had:

.frame(width: .infinity)

instead of:

.frame(maxWidth: .infinity)

Had to do the ol' comment out some lines until it runs trick and then uncomment one-by-one to find it.
UIImpactFeedbackGenerator seems to break SwiftUI Previews in Xcode 12
 
 
Q