Objective c Swift中未识别目标-C枚举

Objective c Swift中未识别目标-C枚举,objective-c,xcode,swift,Objective C,Xcode,Swift,我有一个将枚举作为参数传递的委托方法: func gestureRecognizer(gestureRecognizer: JTTableViewGestureRecognizer!, commitEditingState state: JTTableViewCellEditingState, forRowAtIndexPath indexPath: NSIndexPath!) -> Void { //.... } 枚举为JTTableViewCellEditingState。它

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

func gestureRecognizer(gestureRecognizer: JTTableViewGestureRecognizer!, commitEditingState state: JTTableViewCellEditingState, forRowAtIndexPath indexPath: NSIndexPath!) -> Void {
    //....
}
枚举为JTTableViewCellEditingState。它的实现与委托方法位于相同的头文件中。内容如下:

typedef enum {
    JTTableViewCellEditingStateMiddle,
    JTTableViewCellEditingStateLeft,
    JTTableViewCellEditingStateRight,
} JTTableViewCellEditingState;
但是,尝试引用状态(例如Left)会出现错误:

if state == JTTableViewCellEditingState.Left {
“JTTableViewCellEditingState.Type”没有名为“Left”的成员

像某种农民一样,试图用旧的、客观的方法来做这件事,给了我一个不同的、更令人期待的错误:

if state == JTTableViewCellEditingStateLeft {
无法使用类型为“JTTableViewCellEditingState,JTTableViewCellEditingState”的参数列表调用“==”


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

您可以改用NS_枚举吗?然后尝试JTTableViewCellEditingState.JTTableViewCellEditingStateLeft或甚至.JTTableViewCellEditingStateLeft访问您的枚举。如果无法将其更改为NS_ENUM,请查看

是否可以改用NS_ENUM?然后尝试JTTableViewCellEditingState.JTTableViewCellEditingStateLeft或甚至.JTTableViewCellEditingStateLeft访问您的枚举。如果您无法将其更改为NS_ENUM,请查看

这种类型的ENUM删除会导致swift出现问题。我也有类似的问题。我的解决方案是创建一个助手objective-c方法,该方法进行比较,并在需要==时在swift中使用该方法

另一个解决方案可能是重构代码(如果可以的话),并在objective-c中将其转换为正确的枚举declaration

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

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

另一个解决方案可能是重构代码(如果可以的话),并在objective-c中将其转换为正确的枚举declaration

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

在我的环境-Xcode版本6.1.1 6A2006中:

typedef enum {
    JTTableViewCellEditingStateMiddle,
    JTTableViewCellEditingStateLeft,
    JTTableViewCellEditingStateRight,
} JTTableViewCellEditingState;
导出到Swift的格式为:

struct JTTableViewCellEditingState {
    init(_ value: UInt32)
    var value: UInt32
}
var JTTableViewCellEditingStateMiddle: JTTableViewCellEditingState { get }
var JTTableViewCellEditingStateLeft: JTTableViewCellEditingState { get }
var JTTableViewCellEditingStateRight: JTTableViewCellEditingState { get }
因此,这应该是可行的:

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

在我的环境-Xcode版本6.1.1 6A2006中:

typedef enum {
    JTTableViewCellEditingStateMiddle,
    JTTableViewCellEditingStateLeft,
    JTTableViewCellEditingStateRight,
} JTTableViewCellEditingState;
导出到Swift的格式为:

struct JTTableViewCellEditingState {
    init(_ value: UInt32)
    var value: UInt32
}
var JTTableViewCellEditingStateMiddle: JTTableViewCellEditingState { get }
var JTTableViewCellEditingStateLeft: JTTableViewCellEditingState { get }
var JTTableViewCellEditingStateRight: JTTableViewCellEditingState { get }
因此,这应该是可行的:

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