< 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")
}
}
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.