class Base: UIViewController {
var rightButtonColor: UIColor = UIColor.blueColor()
}
class SecondViewController: Base {
override var rightButtonColor: UIColor {
return UIColor.redColor()
}
}
I'm getting an error:
Getter for 'rightButtonColor' with Objective-C selector 'rightButtonColor' conflicts with getter for 'rightButtonColor' from superclass 'Base' with the same Objective-C selector
Classes in Swift can call and access methods, properties, and subscripts belonging to their superclass and can provide their own overriding versions of those methods, properties, and subscripts to refine or modify their behavior.
Swift version: 5.6. The override is used when you want to write your own method to replace an existing one in a parent class. It's used commonly when you're working with UIViewControllers , because view controllers already come with lots of methods like viewDidLoad() and viewWillAppear() .
An extension can't be overridden because the way Swift implement extension is using static dispatch which means its resolved at compile time.
Try like this:
class Base: UIViewController {
var rightButtonColor: UIColor {
return UIColor.blueColor()
}
}
class SecondViewController: Base {
override var rightButtonColor: UIColor {
return UIColor.redColor()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With