Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to override a property in a subclass in Swift?

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

class A {

    var someStoredProperty : Int? 

}

Class B

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 ?

Update

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

}
like image 811
the_critic Avatar asked Sep 26 '14 11:09

the_critic


People also ask

Can we override stored properties in Swift?

Swift rules state that any inherited property can be overridden whether it is implemented as a stored or computed property at source (parent class).

What is overriding method in Swift?

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

How do you override a variable in Swift?

We use the override keyword to declare method overriding. For example, class Vehicle { func displayInfo(){ ... } } class Car: Vehicle { // override method override func displayInfo() { ... } }

How do I override a Swift extension?

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.

What is override init?

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.


1 Answers

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
        }
    }
}
like image 80
Rob Napier Avatar answered Nov 15 '22 17:11

Rob Napier