I also stumbled upon this question, however, there is no definitive answer
"Ambiguous use of 'propertyName'" error given overridden property with didSet observer
Problem: I would like to override a property in a subclass.
Let me illustrate the problem with an example:
I have a class called A
and a subclass thereof called B
.
class A {
var someStoredProperty : Int?
}
class B : A{
override var someStoredProperty : Int?{
willSet{
//add to superclass's setter
someStoredProperty = newValue! + 10
}
}
}
As soon as I try to set the inherited property of B
var b = B()
b.someStoredValue = 10 // Ambiguous use of someStoredProperty
the compiler tells me
Ambiguous use of someStoredProperty
Why is that ?
class TableViewRow{
typealias ClickAction = (tableView:UITableView, indexPath:NSIndexPath) -> Void
var clickAction : ClickAction?
}
class SwitchTableViewRow: TableViewRow {
override var clickAction : ClickAction? {
didSet{
//override setter
}
}
}
Usage:
var switchRow = SwitchTableViewRow()
switchRow.clickAction = {
//^
//|
//|
//ambiguous use of clickAction
[unowned self, unowned switchRow] (tableView: UITableView, indexPath: NSIndexPath) in
//do something
}
Swift rules state that any inherited property can be overridden whether it is implemented as a stored or computed property at source (parent class).
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() .
We use the override keyword to declare method overriding. For example, class Vehicle { func displayInfo(){ ... } } class Car: Vehicle { // override method override func displayInfo() { ... } }
An extension can't be overridden because the way Swift implement extension is using static dispatch which means its resolved at compile time. Ok, actually above code can be compiled using some tricks if it is your goal to make the code compile.
Overriding init()Override the class initializer init() to initialize or allocate resources for the servlet instance's life, such as a counter. The init() method runs after the servlet is instantiated but before it accepts any requests.
I don't get that error in 6.1, but the underlying problem is you have an infinite loop here. What you meant to say is:
// This is wrong, but what you meant
override var someStoredProperty: Int? {
willSet {
super.someStoredProperty = newValue! + 10
}
}
Note the super
. (Which is yet another reason I strongly recommend using self.
on properties, to make it clear when these infinite loops exist.)
But this code is meaningless. Before setter, you set the value to x + 10
. You then set the value to x
. What you really meant was:
override var someStoredProperty: Int? {
didSet {
if let value = someStoredProperty {
super.someStoredProperty = value + 10
}
}
}
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