【发布时间】: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