Simple SwiftUI ?: Button that Adds

I am trying to learn SwiftUI by myself. I've been stuck on this for a few hours now. Is the sytax wrong?


- I have an integer called x = 1

- want to make a Button that adds 10 to x


This is what i have now, but its giving me error: '()' is not convertible to '() -> Void'

var x: Int = 1
                Button(action: self.x = x + 10) {
                    Text("+10")
                }
Answered by jemmons in 401803022

the `action:` param is looking for a `()->Void` (a function that takes no params and returns nothing), but you're giving it a statement. You should wrap the statement in a closure to make it fit the requested type:


Button(action: {self.x = x + 10}) { 
  Text("+10") 
}
Accepted Answer

the `action:` param is looking for a `()->Void` (a function that takes no params and returns nothing), but you're giving it a statement. You should wrap the statement in a closure to make it fit the requested type:


Button(action: {self.x = x + 10}) { 
  Text("+10") 
}
Simple SwiftUI ?: Button that Adds
 
 
Q