【问题标题】:How to use Enums as parameters in Swift Protocols optional functions如何在 Swift 协议可选函数中使用枚举作为参数
【发布时间】:2015-02-18 17:46:51
【问题描述】:

我在 Swift 中有这个枚举

enum Direction: Int{
  case Left2Right = 0, Right2Left
}

还有这个协议

@objc protocol CellDelegate : NSObjectProtocol{
   optional func has(direction:SwipeDirection) -> Bool
}

我收到此错误 Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C

谁能告诉我为什么会出现这个错误以及如何解决它? 谢谢!

【问题讨论】:

    标签: ios swift enums protocols


    【解决方案1】:

    @objc 属性使协议与 Objective C 兼容(即可用)。但 swift 枚举(在 1.2 beta 之前)在 Objective C 中不可用。因此您不能在该协议中使用 swift 枚举。

    我认为最好的解决方案是使用 swift 1.2 - 它仍处于测试阶段(截至今天),但可以将 @objc 属性应用于 swift 枚举。

    【讨论】:

    • 感谢您的回答。我放了@obj 属性,因为没有它我会出错。
    • 'optional' 只能应用于@objc 协议的成员
    • 那么我建议使用 swift 1.2 - 它仍处于测试阶段,但您可以将@objc 应用于枚举...这将解决您的问题
    • 我可以将应用程序提交到使用 swift 1.2 构建的 Appstore 吗?
    • UIKit 类、协议等是从目标 C 桥接的,它们不是用 swift 编写的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多