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
classkeyword 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:
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.
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
productproperty 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
productproperty 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!