< How to fix “Modifying state during view update, this will cause undefined behavior” | How to fix “Fatal error: No ObservableObject of type SomeType found” > |
Updated for Xcode 14.2
This is a particularly confusing problem, and it occurs because SwiftUI relies heavily on opaque return types. The problem happens because of code like this:
struct ContentView: View {
var body: View {
Text("SwiftUI")
}
}
It feels like such a tiny thing, but the body
property is declared as returning View
rather than some View
. The difference is small but important – you can find out more about it in my article How to use opaque return types in Swift 5.1.
So, to fix the problem use this instead:
struct ContentView: View {
var body: some View {
Text("SwiftUI")
}
}
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.