Swift Beginner
I want to subtract 1 from a label with value "26" every time the "deal" button is pressed. Do I convert it to an Int? And how do I add calculations to it?
I coded cardsAmount.text = "25", which works. But it needs to subtract 1 every time the button is pressed and not assign a hardcoded value.
Also, in the future I would like to add to the number.
Thanks!
class ViewController: UIViewController {
@IBOutlet weak var leftPile: UIImageView!
@IBOutlet weak var rightPile: UIImageView!
@IBOutlet weak var cardsAmount: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func deal(_ sender: Any) {
let shuffled = (2...53).shuffled()
let leftNumber = shuffled[0]
let rightNumber = shuffled[1]
//cardsAmount.text = "25"
leftPile.image = UIImage(named: "c\(leftNumber)")
rightPile.image = UIImage(named: "c\(rightNumber)")
}
.
.
.
}
I think you should add a
variableas you are a beginner. Use this:You can follow the same approach to add a number. If you want to save the
initialNumberthen you can writeinitialNumber = initialNumber + 1and then use it. Do let me know if you have any query regarding this answer.