Swift:麻烦使用在Objective-C中声明的枚举,在Swift中

Chr*_*all 6 enums objective-c swift

好.当我输入问题时查看了各种答案,并没有看到列出的答案.

这似乎是一个基本的,根本性的问题,我必须做错事,但我被驱使疯了(实际上,并不是一个"驱动器".更多的是一个短推杆)试图找出我的错误.

我创建了一个可以在这里下载的测试项目(小项目).

无论如何,我在处理导入Swift的大型Objective-C SDK时遇到了这种情况.

一切都很好.直到...我尝试对C中声明的枚举进行比较.

显然,C枚举不会变成Swift枚举,但我无法弄清楚如何使用它们.

以下是您将在测试项目中看到的示例;

我有几个声明枚举的C文件:

#import <Foundation/Foundation.h>

typedef enum
{
    StandardEnumType_Undef  = 0xFF,
    StandardEnumType_Value0 = 0x00,
    StandardEnumType_Value1 = 0x01,
    StandardEnumType_Value2 = 0x02
} StandardEnumType;

typedef NS_ENUM ( unsigned char, AppleEnumType )
{
    AppleEnumType_Undef  = 0xFF,
    AppleEnumType_Value0 = 0x00,
    AppleEnumType_Value1 = 0x01,
    AppleEnumType_Value2 = 0x02
};

StandardEnumType translateIntToEnumValue ( int inIntValue );
int translateEnumValueToInt ( StandardEnumType inValue );

AppleEnumType translateIntToAppleEnumValue ( int inIntValue );
int translateAppleEnumValueToInt ( AppleEnumType inValue );
Run Code Online (Sandbox Code Playgroud)

所提到的功能几乎与它在锡上所说的完全相同.我不会包括他们.

我做了桥接标题和所有这些.

我试图在Swift应用程序的初始加载中使用它们:

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool
    {
        let testStandardConst:StandardEnumType = translateIntToEnumValue ( 0 )

//        Nope.
//        if ( testStandardConst.toRaw() == 0 )
//        {
//            
//        }

//        This don't work.
//        if ( testStandardConst == StandardEnumType_Value0 )
//        {
//            
//        }

//        Neither does this.
//        if ( testStandardConst == StandardEnumType_Value0.toRaw() )
//        {
//            
//        }

//        Not this one, either.
//        if ( testStandardConst.toRaw() == StandardEnumType_Value0 )
//        {
//            
//        }

        let testAppleConst:AppleEnumType = translateIntToAppleEnumValue ( 0 )

//        This don't work.
//        if ( testAppleConst == AppleEnumType.AppleEnumType_Value0 )
//        {
//            
//        }

//        Neither does this.
//        if ( testAppleConst == AppleEnumType.AppleEnumType_Value0.toRaw() )
//        {
//            
//        }

//        Nor this.
//        if ( testAppleConst == .AppleEnumType_Value0 )
//        {
//            
//        }

        return true
    }
Run Code Online (Sandbox Code Playgroud)

我似乎无法得到$#@ !! 枚举与int或其他任何内容进行比较.

我经常因为我犯的愚蠢错误而感到谦卑,有时需要他们向我指出,所以请谦卑我.

谢谢!

Kee*_*nle 7

typedef NS_ENUM在swift中使用枚举时,你应该在比较和赋值时省略枚举名称,在这里你可以比较它:

if ( testAppleConst == .Value0 ) { ... }
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读有关Swift的Apple do s中的更多信息.