Canvas crashing

Has anyone else noticed that the Canvas / Preview pane crashes when a view contains an @FocusState variable? I also submitted feedback to Apple as I think this is an Xcode bug. But I wondered if anyone else was experiencing a crash in Xcode 13 when you look at the RegisterView in preview?

I’m in that chapter now and it is crashing for me. As you said, it seems to be the @FocusState variable that does it.

1 Like

Hi,

it looks like an Xcode bug indeed — I have the same crash in 13.2, but it works just fine in 13.1.

I have no idea what the underlying problem is nor why this gets around the problem:

In the <Whatever>_Previews structure, wrap the ContentView() in a ZStack()

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

I’m opening the Chapter 07 Starter Project in Xcode 13.1 and canvas continues to crash

I don’t know if this is the proper solution, but I just changed the preview in the KuchiApp file to look like this

struct KuchiApp_Previews: PreviewProvider {
    static let user = UserManager(name: "Ray")
    
    
    static var previews: some View {
        RegisterView()
            .environmentObject(user)
    }
}

This worked for me! Thank you!!! It was so frustrating. You saved me . Thank you!

Thanks @raghavm — you’re right, I forgot to update the previews in KuchiApp.

I’ve just updated projects and book.

My problem is not that the canvas/preview crashes. It simply ignores me. It’s not available on one of my projects though it used to be. I have looked everywhere but I cannot get a canvas. Is this a new xcode or SwiftUI problem or have I accidentally thrown a switch somewhere?

Here’s my code. Nothing fancy

struct ContentView_Previews: PreviewProvider {
   @StateObject var constants = Constants()
   @StateObject var viewModel = ViewModel()

   static var previews: some View {
      ContentView()
   }
}

Curiously, in the same project, a second preview works without a hitch:

struct HelpView_Previews: PreviewProvider {
    static var previews: some View {
        HelpView()
    }
}

you can hide the canvas, but that affects all the project’s views, so probably not the problem in your case. I searched in the console app to see if any issue got logged, but didn’t find anything.

maybe try this: https://twitter.com/_inside/status/1519328061904363527

Followed your link but don’t understand it.
I rebuilt the whole thing and the canvas works. Cannot see that I did anything different.

I have a nice little app now that doesn’t play Wordle but assists a Wordle player. I’d like to put it on the AppStore but am afraid of getting bogged down in form filling and tons of administrative stuff. How hard is it to get past the Apple watchdogs?

there’s a book: https://www.raywenderlich.com/books/ios-app-distribution-best-practices

there was a wordle-like app, lil wordle, that was released as a TestFlight link. Unfortunately, the beta expired a couple of weeks ago.