2

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!

Q8i
  • 1,767
  • 17
  • 25
  • To answer some part of the “why” part of your question: NSCell has two init methods: `initTextCell:` and `initImageCell:`. Those don’t follow Cocoa naming conventions because they don’t have a “with” as part of their name. I think that’s what confuses the Swift compiler and makes it print something about `initWithTextCell:`. But I have no idea why this only happens in sub-subclasses. – Marco Masser May 20 '15 at 13:55

1 Answers1

2

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 {}
Casey Fleser
  • 5,707
  • 1
  • 32
  • 43
  • 1
    Works fine for me! Thanks a bunch. The error message was so misleading I did not think of this at all. – Q8i May 04 '15 at 15:39