Reputation: 5441
Currently we have a cell with 5 buttons, when one button is pressed it will turn white while all other buttons will turn black. There is a separate checkmark imageview on the button that will be checked or unchecked depending if the button is selected. So fat this works for the imageView:
@IBAction func changeColor(_ sender: Any) {
guard let button = sender as? UIButton else {
return
}
self.selectedCheckMarkImageView1.isHidden = button.tag != 1
self.selectedCheckMarkImageView2.isHidden = button.tag != 2
self.selectedCheckMarkImageView3.isHidden = button.tag != 3
self.selectedCheckMarkImageView4.isHidden = button.tag != 4
self.selectedCheckMarkImageView5.isHidden = button.tag != 5
}
but when trying to use the same approach for changing button image:
button.setImage(UIImage(named: "LR - Nano White - Selected")!, for: .normal) = button.tag != 1
an error occurs:
Cannot assign to value: function call returns immutable value
I tried putting it in a switch statement like this:
switch (button.tag) {
case != 1: //error occurs here
default:
//
}
but get the error:
'!=' is not a prefix unary operator
Any idea on how to solve this issue?
Upvotes: 0
Views: 90
Reputation: 347
Try this
switch (button.tag) {
case _ where button.tag != 1:
default:
//
}
Upvotes: 1
Reputation: 63167
A switch is a replacement for 1 value being compared against a fix set of multiple values other values, to pick a single branch of execution to take. You have a situation in which multiple values are checked against multiple different values, and doing multiple different things in response. A single switch can't really replace that.
Instead, I suggest reduce repetition by using a for
loop.
let selectedCheckMarkImageViews = [
selectedCheckMarkImageView1,
selectedCheckMarkImageView2,
selectedCheckMarkImageView3,
selectedCheckMarkImageView4,
selectedCheckMarkImageView5,
]
let visbleImageViewIndex = button.tag // TODO: either number these 0..<4, or add 1 below
for (index, view) in selectedCheckMarkImageViews.enumerated() {
view.isHidden = visbleImageViewIndex != index
}
Upvotes: 2