Swift 2, I have a class inherits from objc's UIView and it has 'on' variable, and related methods 'setOn:animated' and 'setOn:' like below:
public class AView: UIView {
var on: Bool = false
public func setOn(on: Bool, animated: Bool) {
self.on = on
// do something more about animating
}
public func setOn(on: Bool) {
setOn(on, animated: false)
}
And I got an error message: method 'setOn' with Objective-C selector 'setOn:' conflicts with setter for 'on' with the same Objective-C selector
I think willSet
or didSet
is not a solution because setOn:animated:
is called twice even if I add some guard conditions:
var on: Bool = false {
willSet {
if self.on != newValue {
setOn(self.on, animated: false)
}
}
}
....
....
let a = AView()
a.setOn(true, animated: true) // setOn:animated: is called twice
Is there a solution without changing a variable name and methods name?
Workaround: My solution is add extra internal variable and expose it with computed property. I don't like adding extra variable and definitely there will be a better solution.
private var isOn: Bool = false
var on: Bool {
set(newOn) {
setOn(newOn, animated: false)
}
get {
return isOn
}
}
public func setOn(on: Bool, animated: Bool) {
self.isOn = on
// do something ...
}