Swift override instance variables

2019-02-02 20:44发布

I know this answer has already been posted in some other form here, but i'd like to understand more about overriding instance variables in swift.

Let's say i have this code

class BaseView:UIView{
 let someVariable:Int = 1
 // do some work with someVariable
}

class ExtendedView:BaseView{
 let someVariable:Int = 2
}

Ok. From what i read, the constant requires an override prefix. Other answers said that i should declare the setter and getter? Why? I really don't care about those two. I just need the value replaced. I can't really use the init override because i'm inheriting from UIView and this might be quite dangerous (i think).

Any suggestions are welcomed.

1条回答
爷、活的狠高调
2楼-- · 2019-02-02 21:31

As you say, you cannot simply redefine a constant in a subclass (it is a constant, after all). The error you get is "Cannot override with a stored property". It does appear to be possible to override a var, however, when I change the let someVariable to var someVariable I get "ambiguous use of 'someVariable'" when I access it in the subclass (note - same thing happens whether I use override or not).

The simplest solution is to use a getter. This is really a function, so you can happily override it, the backing variable will be managed for you, and if you don't supply a setter ... it will be constant for each class:

class BaseView: UIView {
    var someVariable: Int { get { return 1 } }
    // do some work with someVariable
}

class ExtendedView: BaseView {
    override var someVariable: Int { get { return 2 } }
}

let a = BaseView()
a.someVariable // 1
let b = ExtendedView()
b.someVariable // 2

As commentator @user3633673 points out, if you only have a getter (and not a setter), you can drop the get, but I left it in for clarity of the principle. Here's the same without it...

class BaseView: UIView {
    var someVariable: Int { return 1 }
    // do some work with someVariable
}

class ExtendedView: BaseView {
    override var someVariable: Int { return 2 }
}

let a = BaseView()
a.someVariable // 1
let b = ExtendedView()
b.someVariable // 2
查看更多
登录 后发表回答