I'm trying to show an alert in Swift based on a computed property. Basically, whenever a user clicks on a button the value of "round" is updated. When more than 10 rounds have taken place, an alert shows.
To do this, I've created a boolean variable called "showingAlert". This has to be a @State var so that it gets set to false again when the user closes the alert.
However, the compiler tells me that a property wrapper like @State "cannot be applied to a computed property" :-(
This is the code that I've tried:
@State var round = 0
@State var showingAlert:Bool {round > 10 ? true : false}
func result(player: Int, app: Int) {
if player > app {
round += 1
}
else {
round += 1
}
}
var body: some View {
Button(action: {self.result(player: 1, app: 1)}) {
Text("Button")
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("title"), message: Text("message"), dismissButton: .default(Text("Continue"))
)
}
Is there any way round this? I'd love to create an alert that shows without error messages.
round > 10 ?...
code to directly after the if/else?showingAlert = round > 10 ? true : false
. By the way, your if and else is doing the same thing – Joakim Danielson