4

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.)

rsc
  • 10,348
  • 5
  • 39
  • 36
  • possible duplicate of [Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector](http://stackoverflow.com/questions/29457720/compiler-error-method-with-objective-c-selector-conflicts-with-previous-declara) – Caleb Kleveter Sep 23 '15 at 16:05

2 Answers2

7

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")
    }
  }
}
Nikita Leonov
  • 5,684
  • 31
  • 37
1

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){

Community
  • 1
  • 1
  • Thanks for your answer, but I mentioned that I don't want to change the name of the function. I want to understand from where this name collision is happen, and if in fact here is name collision (which I couldn't actually find it in the code), I want to fix it. – rsc May 08 '15 at 16:06
  • @RSC Did you find the reason why this is happening? – kerry Sep 16 '15 at 06:27