J_llama
J_llama

Reputation: 113

Variable with getter/setter cannot have initial value, on overridden stored property

When creating a stored property with Observing Accessors, I can specify a default value. However, when overriding a stored property and its Accessors I cannot specify a default value.

Variable with getter/setter cannot have initial value.

Which seems very strange, as this is NOT a computed property with a getter/setter, but a set of Observing Accessors on a stored property!

class FirstViewController: UIViewController {

internal var test: Float = 32.0 {
    willSet {

    }

    didSet {

    }
}

The first view controller compiles fine, with a stored property initialized to 32.0

class SecondViewController: FirstViewController {

override var test: Float = 64.0 {
    willSet {

    }

    didSet {

    }
}

The second view controller does not compile, as the 'computed property' is being given an initial value

Upvotes: 9

Views: 8366

Answers (2)

AD Progress
AD Progress

Reputation: 5086

I know that this has been asked a long time ago but I came up with a slightly different solution and it works exactly as you wanted. You have a property in the first ViewController then in the inherited one you override it and have observers set on it in the form of didSet.

So in the FirstViewController you have a property like in the example below:

var myNumber: Double = 20.00

Then in the SecondViewController which inherits from FirstViewController you override it as follows:

override var myNumber: Double {
     didSet {
             //Here you can update UI or whatever you want to do once the property changes
             //Print its value
             print("Value of myNumber is : \(myNumber)")
            }

I hope this will help someone with the above issue as this is a nice and easy way to solve the problem mentioned above.

Upvotes: 1

Yury
Yury

Reputation: 6114

In swift you are able to override properties only with computed properties (which are not able to have default values) with same type. In your case, if you wish override test property in SecondViewController you need write something like this:

override var test: Float {
    get {
        return super.test
    }
    set {
        super.test = newValue
    }
}

And there is no way to override didSet/willSet observers directly; you may do this by write other methods invoked in observers and just override them:

FirstViewController:

internal var test: Float = 32.0 {
    willSet {
        test_WillSet(newValue)
    }
    didSet {
        test_DidSet(oldValue)
    }
}

func test_WillSet(newValue: Float) {}
func test_DidSet(oldValue: Float) {}

SecondViewController:

override func test_WillSet(newValue: Float) {
    super.test_WillSet(newValue)
}
override func test_DidSet(oldValue: Float) {
    super.test_DidSet(oldValue)
}

Upvotes: 10

Related Questions