2015-05-04 55 views
2

爲什麼我不能繼承NSCell的任何子類? 我想繼承MyButtonCell,這是CustomButtonCell: NSButtonCell。含義Swift 1.2爲什麼我不能繼承任何NSCell的子類?

class MyCustomButtonCell: MyButtonCell { } 

總是給我下面的錯誤:

<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?) 
        ^

簡單的步驟來重現我的問題:

  1. 打開終端

  2. 類型:swift(如果你有最新的Xcode 6.3.1)

  3. 當你得到,

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

類型如下:

1> import AppKit 
2> class Foo : NSCell {} 
3> class Bar : Foo {} 
  • 你會得到這些錯誤:
  • 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?) ^

    爲什麼?有沒有解決這個問題的方法?

    僅供參考:Swift 1.1沒有這個問題!

    +0

    要回答你的問題的「爲什麼」部分的某些部分:的NSCell有兩種初始化方法:'initTextCell:'和'initImageCell:'。那些不遵循Cocoa命名約定,因爲它們沒有「with」作爲其名稱的一部分。我認爲這就是Swift編譯器的混淆,並使得它打印出一些關於'initWithTextCell:'的內容。但我不知道爲什麼這隻發生在子子類中。 –

    回答

    2

    看來,並稱符合NSCoding的init方法去除錯誤:

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

    適合我!謝謝一堆。錯誤信息太具有誤導性,我完全沒有想到這一點。 – Q8i

    相關問題