2

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()

heimi
  • 499
  • 7
  • 16
  • See my message: https://stackoverflow.com/questions/44377881/create-a-subclass-of-nsview-to-enable-settag/51529788#51529788. This is probably what you are after – cyanide Jul 26 '18 at 01:50

1 Answers1

4

In Swift you can't directly override the permissions of a property, but you can do whatever you like in custom-defined setters and getters:

Swift 4.0

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

Note: _tag is just a variable name that implies privateness, but it could be any other name you like and doesn't have to have an underscore at the beginning.