WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Day 19 HWS+ Advanced Solution @FocusState caused failure in Preview "Cannot preview in this file - Converter crashed"

Forums > 100 Days of SwiftUI

When I open Paul's advanced solution in Xcode, the preview window displays "Cannot preview in this file - Converter crashed". I've narrowed the source of the problem down to

    @FocusState private var inputIsFocused: Bool

When I comment out this line (and all the lines that are dependent on it) the preview error goes away.

When the line, and the dependents, are live, the project builds and runs just fine, but causes the error in Preview.

Anybody got any ideas on how to fix the problem?

1      

What version of Xcode are you using?

What OS version is your project targeting?

I cannot reproduce this issue.

1      

Thanks for taking an interest.

Xcode v13.2.1 Target iPhone iPad 15.0

I can start with a new SwiftUI project, add the @FocusState and get the error.

Once I narrowed down the source of the problem, I did some additional web searching and found a workaround:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ZStack {
            ContentView()
        }
    }
}

Adding the ZStack made the error in the canvas go away.

2      

Had similar problem, but on Day 18. XCode v13.2.1 target iPhone iPad 15.2 Thanks for the solution! Wish I understood why this fixed it.

Afterwards, wound up removing ContentView_Previews struct entirely, hiding canvas, and using only simulator.

1      

I had the same issue, I figured that something needed to be added to the preview to make it work. That generally has been the issue for me when previews crash.

Thanks for the solution!

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.