SwiftUI Preview crashes on simple code

Entering the following simple code, in a default SwiftUI iOS project, causes the preview to consistently crash:

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
            padding()
            Text("Another Line")
        }
    }
}

I know "padding()" should be "spacer()", but this simple syntax error should just show an error, not crash the preview

Replies

"padding()" should be ".padding()"

As in:

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                .padding()
            Text("Another Line")
        }
    }
}

"padding()" is an instance method, so calling it without the leading ".", as you do, probably causes something horrible to happen, which causes the crash.
(Since it will return a function, not a View)

  • Yes, I understand that I was using the wrong code, I wasn't trying to add padding to the first Text view, but confused "padding()" with "spacer()". My point is, that "simple" code mistake should not take down the whole preview with a crash. A nice, error message would be more helpful. Because of the crashing, it took much longer to figure out what my mistake was. If the preview/SwiftUI is so sensitive to small syntax errors, how does anyone get any work done? ;)

Add a Comment