Stanislav K.
Stanislav K.

Reputation: 424

Override property declared in framework

Is it possible to change some variable in my app declared in framework?

I have a class in framework (i.e module) which installed as Swift Package. Lets name it FooSDK

open class Foo {
    open var someProperty: String = "Monday"
}

And class in my app:

import FooSDK

class MyFoo: Foo {
    override var someProperty = "Sunday"
}

In that case on the override line Xcode shows error:

Overriding non-open property outside of its defining module

But property is open, so I can't understand what's the problem.

Another error on same line:

Cannot override with a stored property 'someProperty'

Why it can't?

Upvotes: 0

Views: 599

Answers (1)

Rob Napier
Rob Napier

Reputation: 299663

You generally cannot override with a stored property. In this case it's a bit tricker, because the value is really just a default (since this is var). Generally you would override with a computed property:

private var _someProperty: String = "Sunday"
override var someProperty: String { get { _someProperty } 
                                    set { _someProperty = newValue } }

Or set the value in the initializer:

override init() {
    super.init()
    someProperty = "Sunday"
}

Upvotes: 1

Related Questions