如何在Swift协议可选函数中使用枚举作为参数

时间:2022-07-28 18:00:35

I have this enum in Swift

我的这个enum是Swift的。

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

And this protocol

这个协议

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

I am getting this error Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C

我得到这个错误方法不能被标记为@objc,因为参数的类型不能在Objective-C中表示

Can anybody tell me why do i get this error and how to fix it please? Thanks!

有人能告诉我为什么会出现这个错误吗?谢谢!

1 个解决方案

#1


7  

The @objc attribute makes the protocol compatible (i.e. usable) with Objective C. But swift enums (prior to 1.2 beta) are not available in Objective C. So you cannot use a swift enum in that protocol.

@objc属性使协议与Objective c兼容(即可用),但是swift enums(在1.2 beta之前)在Objective c中不可用,因此不能在该协议中使用swift enum。

I think that the best solution is to use swift 1.2 - it's still in beta (as of today), but it's possible to apply the @objc attribute to swift enums.

我认为最好的解决方案是使用swift 1.2——它仍然处于beta状态(到今天为止),但是可以将@objc属性应用到swift enums。

#1


7  

The @objc attribute makes the protocol compatible (i.e. usable) with Objective C. But swift enums (prior to 1.2 beta) are not available in Objective C. So you cannot use a swift enum in that protocol.

@objc属性使协议与Objective c兼容(即可用),但是swift enums(在1.2 beta之前)在Objective c中不可用,因此不能在该协议中使用swift enum。

I think that the best solution is to use swift 1.2 - it's still in beta (as of today), but it's possible to apply the @objc attribute to swift enums.

我认为最好的解决方案是使用swift 1.2——它仍然处于beta状态(到今天为止),但是可以将@objc属性应用到swift enums。