gpt4 book ai didi

Swift 无法识别 Objective-C 枚举

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:19 24 4
gpt4 key购买 nike

我有一个将枚举作为参数传递的委托(delegate)方法:

func gestureRecognizer(gestureRecognizer: JTTableViewGestureRecognizer!, commitEditingState state: JTTableViewCellEditingState, forRowAtIndexPath indexPath: NSIndexPath!) -> Void {
//....
}

枚举是 JTTableViewCellEditingState。它的实现与委托(delegate)方法在同一个头文件中。如下:

typedef enum {
JTTableViewCellEditingStateMiddle,
JTTableViewCellEditingStateLeft,
JTTableViewCellEditingStateRight,
} JTTableViewCellEditingState;

然而尝试引用一个状态,例如 Left,会给出一个错误:

if state == JTTableViewCellEditingState.Left {

'JTTableViewCellEditingState.Type' does not have a member named 'Left'

尝试用旧的、Objective-C 的方式来做,就像某种农民一样,给了我一个不同的、更意料之中的错误:

if state == JTTableViewCellEditingStateLeft {

Cannot invoke '==' with an argument list of type '(JTTableViewCellEditingState, JTTableViewCellEditingState)'

我想知道我应该如何克服这个问题?我相信引用 Objective-C 枚举在过去工作得很好。

最佳答案

这种类型的枚举减速会导致 swift 出现问题。我有类似的问题。我的解决方案是创建一个辅助 objective-c 方法来进行比较,并在需要 == 时 swift 使用该方法。

如果可以的话,其他解决方案可能是重构该代码,并在 objective-c 中将其转换为适当的枚举 decleration。

typedef NS_ENUM(NSInteger, MyEnum) {
MyEnumValue1,
MyEnumValue2
};

关于Swift 无法识别 Objective-C 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27434862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com