objective-cmacosswiftnscell

Swift 1.2 Why cannot I subclass any NSCell's subclass?


Why can't I subclass any subclasses of NSCell? I would like to subclass MyButtonCell, which is CustomButtonCell: NSButtonCell. Meaning

class MyCustomButtonCell: MyButtonCell { }

always gives me the following errors:

<unknown>:0: error: declaration has a different @objc name from the declaration it overrides ('initTextCell:' vs. 'initWithTextCell:')
Custom_Controls.MyButtonCell:9:24: note: overridden declaration is here
  @objc @objc override init(textCell aString: String)
                       ^
<unknown>:0: error: declaration has a different @objc name from the declaration it overrides ('initImageCell:' vs. 'initWithImageCell:')
Custom_Controls.MyButtonCell:10:24: note: overridden declaration is here
  @objc @objc override init(imageCell image: NSImage?)
                       ^

Simple steps to reproduce my problem:

  1. Open Terminal

  2. Type: swift (if you have the latest Xcode 6.3.1)

  3. When you get,

    Welcome to Swift version 1.2. Type :help for assistance.

type the followings:

1> import AppKit
2> class Foo : NSCell {}
3> class Bar : Foo {} 
  1. You'll get these errors:

declaration has a different @objc name from the declaration it overrides ('initTextCell:' vs. 'initWithTextCell:')__lldb_expr_9.Foo:3:24: note: overridden declaration is here @objc @objc override init(textCell aString: String) ^ declaration has a different @objc name from the declaration it overrides ('initImageCell:' vs. 'initWithImageCell:')__lldb_expr_9.Foo:4:24: note: overridden declaration is here @objc @objc override init(imageCell image: NSImage?) ^

Why? Is there a way around this problem?

FYI: Swift 1.1 didn't have this issue!


Solution

  • It seems that adding an init method that conforms to NSCoding removes the error:

    import AppKit
    class Foo : NSCell {
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    }
    class Bar : Foo {}