Override getter/setter problem

I have try to override variable in subclass many times, but never try override getter/setter before today, this is my Swift code in Playground :


class Person {
    var name: String {
        didSet {
            print("Superclass did Set")
        }
        willSet {
            print("Superclass will Set")
        }
    }
    init(name: String) {
        self.name = name
    }
}

class Student: Person {
    override var name: String {
        didSet {
            print("Subclass did Set")
        }
        willSet {
            print("Subclass will Set")
        }
    }
}

let apple = Student(name: "John")
apple.name = "Apple"


The results like below:


Subclass will Set

Superclass will Set

Superclass did Set

Subclass did Set


That's mean we can't override variable setter/getter?

Replies

Looks like you have to define as a class var

see h ttps://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html


For computed type properties for class types, you can use the

class
keyword instead to allow subclasses to override the superclass’s implementation. The example below shows the syntax for stored and computed type properties:


Also found an interesting discussion here:

h ttps://stackoverflow.com/questions/29971528/overriding-getter-in-swift


Hope that helps.

Thanks for you help, maybe it's wrong question, the question should be Why Can't Overriding Superclass Property Observers?

I made some investigations according to the information you provided,


Apple says in https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html, chapter Overriding Property Observers:

You can use property overriding to add property observers to an inherited property. This enables you to be notified when the value of an inherited property changes, regardless of how that property was originally implemented.

There are same discussion here:

https://stackoverflow.com/questions/29503482/override-property-observer


It look likes a Swift bug.



Sorry for multi-reply, illegal character problem.

So, you could file a bug:

- to Apple

- to Swift forum


Thanks for the feedback.

Note: those illegal characters in the forum are really messy

It look likes a Swift bug.

It does? I’m not sure how you draw that conclusion. Property observers are not the same as property implementations, and I can see plenty of situations where I’d want both observers to fire when the property changes.

For example, imagine you have a view controller that displays a product. When the

product
property changes, the property observer updates the view to reflect the new product. Then you subclass that view controller to display the price of that product by looking it up in a price list. That subclass will also want a property observer on the
product
property to updates the price view. The current model suits that use case perfectly.

Now, you could argue that Swift should be enhanced to support your use case as well — and if you want to do that you should totally jump over to Swift Forums and make your case — but the current behaviour seems more like choice that you disagree with than a bug.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for your help!


The conclusion comes from Ovrride keyword.


I using override variable in subclass a lots of time, my intention is override some super class action, if I want excute super class method, super.doSome() is what I expected, but Overriding Property Observers more like extension or KVO, super method always excute, that was some kind of ambiguity.


Anyway, Swift is one of my favorite programming language, I am fan of C-style programming language, Swift too high level for me, lots of action is too abstract, although I have learned it for a few years.


Thanks again, eskimo, have a good day!