I have a default SwiftUI project for iOS. I want to run that on Apple Silicon macOS.
Here is my ContentView
import SwiftUI
struct ContentView: View {
@StateObject var stateObj = StateObj()
@State var showTest = false
var body: some View {
Button(action: {
showTest = true
})
{
Text("Test Button")
}
.sheet(isPresented: $showTest) {
FailingView()
}
.environmentObject(stateObj)
}
}
struct FailingView: View {
@EnvironmentObject var stateObj: StateObj
@State private var showAlert = false
var body: some View {
VStack(content: {
Toggle(isOn: $stateObj.toggleState) {
Text("Test")
}
})
.alert(isPresented: $showAlert, content: {
Alert(title: Text("Failure reason"))
})
}
}
class StateObj: ObservableObject {
@Published var toggleState = false
}
Now I run it by selecting My Mac (Designed for iPad) and do the following
Click Test Button.
App fails with Fatal error: No ObservableObject of type StateObj found.
After removin alert method the app works as expected.
Alert method has some strange side effect that causes the app to fail on Mac.
Any ideas?