【问题标题】:Swift: Trouble Using Enums Declared in Objective-C, In SwiftSwift:在 Swift 中使用 Objective-C 中声明的枚举时遇到问题
【发布时间】:2014-07-29 14:10:54
【问题描述】:

好的。我在输入问题时查看了各种答案,但没有看到列出的答案。

这似乎是一个基本的、根本性的问题,我必须做错了什么,但我被逼疯了(实际上,不是一个“驱动器”。更多的是一个短推)尝试找出我做错了什么。

我创建了一个测试项目that can be downloaded here(小项目)。

无论如何,我在导入一个大型的 Objective-C SDK 并写入 Swift 时遇到了这个问题。

一切都很好。直到...我尝试对 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 );

上面提到的功能与它在锡上所说的差不多。我不会包括他们。

我做了桥接头和所有这些。

我正在尝试在 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
    }

我似乎无法获得 $#@!枚举以与整数或其他任何内容进行比较。

我经常为自己犯的愚蠢错误感到谦卑,有时需要向我指出,所以请让我谦虚。

谢谢!

【问题讨论】:

    标签: objective-c enums swift


    【解决方案1】:

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

    if ( testAppleConst == .Value0 ) { ... }
    

    您可以在 Apple do s for Swift here 中阅读更多相关信息。

    【讨论】:

    • 谢谢。我试过了,它不起作用。在 C 中声明的枚举似乎有些古怪。这是一个问题,因为我使用的 C 头文件有几百个标准 C typedef 枚举,使用起来很痛苦。
    • 这很奇怪,因为我已经下载了您分享的项目并在其中尝试过...您使用什么版本的 xCode?​​span>
    • Xcode 6 beta 4。你取消了最后一个例子的注释吗?那是我试过的。无论如何,我正在尝试,以防万一我错过了什么。我得到的是 AppleEnumType 没有名为“AppleEnumType_Value0”的成员。
    • 天啊!如果我删除枚举标头(AppleEnumType_)并留下尾随的枚举内容,它会起作用。这很奇怪。你的回答是正确的,但这也意味着我需要去掉枚举前缀。
    • 好的。我和我的同事讨论过这个问题,我们认为这是有道理的。这是范围的问题。 ObjC 约定的原因是因为所有 C 枚举都在全局范围内,而 Swift 为每个枚举都有一个本地命名空间。这意味着我们不需要将枚举名称作为值的前缀。他们不会翻译标准枚举,这很烦人(但不是破坏交易)。现在我知道问题所在了,我可以忍受它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    相关资源
    最近更新 更多