Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding properties in swift

For example a have a first class

public class MyBaseButton: UIButton {      public var weight: Double = 1.0      public var text: String? {         get {             return self.titleForState(.Normal)         }         set {             return self.setTitle(newValue, forState: .Normal)         }     } } 

And inherited class:

public class SomeButton: SomeBaseButton {      override public var text: String? = "text"     override public var weight: Double = 2.0 } 

So, the logic is that SomeClass define own text and weight. But I'm getting an errors:

For text: "Cannot override with a stored property 'text'"

For weight: "Cannot override with a stored property 'weight'"

like image 834
Vasyl Khmil Avatar asked Feb 28 '15 20:02

Vasyl Khmil


People also ask

What are the properties of method overriding?

When overriding one method with another, the signatures of the two methods must be identical (and with same visibility). In C#, class methods, indexers, properties and events can all be overridden. Non-virtual or static methods cannot be overridden. The overridden base method must be virtual, abstract, or override.

What does override mean 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 method in Swift?

In Swift Inheritance, the subclass inherits the methods and properties of the superclass. This allows subclasses to directly access the superclass members. Now, if the same method is defined in both the superclass and the subclass, then the method of the subclass class overrides the method of the superclass.

Does Swift support method overriding?

There is no overloading. In Swift you can definitely overload.


2 Answers

Interestingly this works just fine in pure Swift classes. For example, this works as expected:

public class FooButton {     public var weight: Double = 1.0 }  public class BarButton: FooButton {     override public var weight: Double = 2.0 } 

The reason it does not work for you is because you are working with Objective-C classes: since UIButton is an Objective-C class, all its subclasses will be too. Because of that, the rules seem to be a bit different.

Xcode 6.3 is actually a bit more informative. It shows the following two errors:

Getter for "weight" overrides Objective-C method "weight" from superclass "FooButton" Setter for "weight" overrides Objective-C method "setWeight:" from superclass "Foobutton"

Since the following does work ...

public class BarButton: FooButton {     override public var weight: Double {         get {             return 2.0         }         set {             // Do Nothing         }     } } 

... my guess is that these methods are simply not synthesized correctly.

I wonder if this is a compiler bug. Or a shortcoming. Because I think it could handle the case.

Maybe the Swift designers thought that in case of overriding weight you could also simply set it to a different value in your initializer. Hm.

like image 184
Stefan Arentz Avatar answered Oct 13 '22 23:10

Stefan Arentz


In addition, if someone wants to override property to have dynamic effect, see KVO

class MyClass: NSObject {     var date = NSDate() }  class MyChildClass: MyClass {     dynamic override var date: NSDate {         get { return super.date }         set { super.date = newValue }     } } 
like image 39
onmyway133 Avatar answered Oct 14 '22 00:10

onmyway133