swift3 MacOS set Tag in subclass of NSView

时间:2017-06-27 14:03:41

标签: swift3 tags nsview

I have a sublass Abszisse of NSView. It works as expected, but I can't set a tag for it, neither programmatically nor in the storyboard where the default value of -1 is grayed out.

class Abszisse: NSView{
override var tag = 0
...


The error is Cannot override with a stored property 'tag'
The docs say to redefine the property as readonly, but I can't find anything how to do that. Might be very simple. In Objective-C that was not a problem. Is there another possibility than in the answer of apineda in
create a subclass of NSView to enable setTag()

中选择列表

1 个答案:

答案 0 :(得分:1)

在Swift中,您无法直接覆盖属性的权限,但您可以在自定义的setter和getter中执行任何操作:

Swift 4.0

var _tag = -1
override var tag: Int {
    get {
        return _tag
    }
    set {
        _tag = newValue
    }
}

注意:_tag只是一个隐含私有性的变量名称,但它可以是您喜欢的任何其他名称,并且不必在开头有一个下划线。