Given the following code, if line 17 isn't present there'll be a crash upon presenting the sheet.
Since content
shouldn't be nil
at the time of presentation, why does this crash?
import SwiftUI
struct ContentView: View {
@State private var isPresented = false
@State private var content: String?
@State private var startTask = false
var body: some View {
Text("Tap me")
.onTapGesture {
startTask = true
}
.task(id: startTask) {
guard startTask else { return }
startTask = false // <===== Crashes if removed
content = "Some message"
isPresented = true
}
.sheet(isPresented: $isPresented) {
Text(content!)
}
}
}
iOS 16.4, Xcode 14.3.1
The question is answered in this post. The correct solution is to use capture lists.