Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method 'setChecked' with Objective-C selector 'setChecked:' conflicts with setter for 'checked' with the same Objective-C selector

I have created custom class that inherits from UIButton.

In that class I have created a function defined as:

  func setChecked(checked:Bool){
        self.checked = checked
        if checked {
            buttonImageView.image = UIImage(named: "radioSelected.png")
        } else {
            buttonImageView.image = UIImage(named: "radioUnselected.png")
        }
    }

which was working fine until I updated my xCode to 6.1.3.

Now I keep getting the error message on the function definition line:

Method 'setChecked' with Objective-C selector 'setChecked:' conflicts with setter for 'checked' with the same Objective-C selector

I already tried to make a override it but then I get a error saying that "Method does not override any method from its superclass".

Anyone know how can I correctly solve it?

(I don't want to change the name of my function.)

like image 341
rsc Avatar asked Feb 11 '23 08:02

rsc


2 Answers

You have function name conflicting with your property. What about to implement it in a more elegant way with property observing? This explicitly shows how value changes, as well as a side effects for the value changes.

class RadioButton: UIButtom {
  var checked: Bool = false {
    didSet {
        buttonImageView.image = UIImage(named: checked ? "radioSelected.png" : "radioUnselected.png")
    }
  }
}
like image 142
Nikita Leonov Avatar answered Feb 16 '23 04:02

Nikita Leonov


It seems that there is a name collision happening. There a few more people with the same issue:

Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector

https://stackoverflow.com/questions/30006724/method-setplayer-with-objective-c-selector-setplayer-conflicts-with-setter

Try renaming your function to something like:

func checkedSetter(checked:Bool){

like image 25
Tinker Bell Avatar answered Feb 16 '23 04:02

Tinker Bell