objective-c - Swift 1.2 为什么我不能继承任何 NSCell 的子类?

标签 objective-c macos swift nscell

为什么我不能继承 NSCell 的任何子类? 我想子类化 MyButtonCell,即 CustomButtonCell: NSButtonCell。含义

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 没有这个问题!

    最佳答案

    看来添加一个符合 NSCoding 的 init 方法可以消除错误:

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

    关于objective-c - Swift 1.2 为什么我不能继承任何 NSCell 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031628/

    相关文章:

    ios - UITableView didDeselectRowAtIndexPath Cell 在未选中时返回 Not on Selected

    objective-c - 当我将图像保存到文件时,保存的方向是错误的

    ios - 如何判断 CollectionViewCell 是否位于屏幕中央

    ios - 如何在swift中从日期选择器获取开始和结束时间?

    objective-c - 带有纬度和经度的 swift 中的 CLLocation 对象

    ios - 自动布局 - 使用约束优先级的一个 Storyboard 的纵向和横向?

    css - iframe 中位置为 "fixed"的元素不会在 Safari 中呈现

    java - 想要隐藏/删除屏幕菜单栏

    macos - 带有图像的 NSButton

    ios - 使用 Swift 的 UICollectionView