Swift:使用在Objective-C中声明的枚举,在Swift中麻烦 [英] Swift: Trouble Using Enums Declared in Objective-C, In Swift
问题描述
行。看到各种答案,当我输出问题,并没有看到答案列出。
这似乎是一个基本的基本问题,我必须做错了事情,但是我被疯狂了(实际上并没有太多的驱动器,更多的是一个简短的推杆)试图弄清楚我出了什么问题。
我已经创建了一个测试项目可以在这里下载(小项目)
无论如何,我遇到这个问题,当我在导入一个我写入Swift的大型Objective-C SDK时。
一切都很好。直到...我尝试对C中声明的枚举进行比较。
显然,C枚举不会变成Swift枚举,但是我无法弄清楚如何使用它们。
以下是您将在测试项目中看到的示例;
我有一对的C文件声明枚举:
#import< Foundation / Foundation.h>
typedef枚举
{
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应用程序(应用程序:UIApplication !, didFinishLaunchingWithOptions launchOptions:NSDictionary!) - > Bool
{
let testStandardConst:StandardEnumType = translateIntToEnumValue(0)
//不,
// if(testStandardConst.toRaw()== 0)
// {
//
//}
//这不'工作
// if(testStandardConst == StandardEnumType_Value0)
// {
//
//}
//这样也不行。
// if(testStandardConst == StandardEnumType_Value0.toRaw())
// {
//
//}
//不是这一个,或者。
// if(testStandardConst.toRaw()== StandardEnumType_Value0)
// {
//
//}
let testAppleConst:AppleEnumType = translateIntToAppleEnumValue(0)
//这不行。
// if(testAppleConst == AppleEnumType.AppleEnumType_Value0)
// {
//
//}
//这样也不行。
// if(testAppleConst == AppleEnumType.AppleEnumType_Value0.toRaw())
// {
//
//}
// Nor这个。
// if(testAppleConst == .AppleEnumType_Value0)
// {
//
//}
return true
}
我似乎无法获得$#@!枚举可以与任何其他内容进行比较。
我经常被愚蠢的错误谦卑,有时候需要他们指出我,所以请谦卑我。
谢谢!
使用 typedef NS_ENUM
枚举在swift中,您应该在比较和分配中省略枚举名称,这里可以比较它:
if(testAppleConst == .Value0){...}
您可以阅读更多关于这个在苹果做的这个Swift 这里。
OK. Looked at the various answers when I typed out the question, and don't see the answer listed.
This seems such a basic, fundamental issue that I MUST be doing something wrong, but I am being driven crazy (actually, not much of a "drive." More of a short putt) trying to figure out what I am getting wrong.
I have created a test project that can be downloaded here (small project).
In any case, I encountered this, when working on importing a large Objective-C SDK that I wrote into Swift.
Everything works great. Until... I try to do comparisons on enums declared in C.
Obviously, C enums are not turned into Swift enums, but I am having trouble figuring out how to use them.
Here's a sample of what you will see in the test project;
I have a couple of C files that declare enums:
#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 );
The functions mentioned do pretty much what it says on the tin. I won't include them.
I did the bridging header and all that.
I am trying to use them in the initial load of the Swift app:
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
}
I can't seem to get the $#@!! enums to compare to either ints or anything else.
I am often humbled by the stupid errors I make, and sometimes need them pointed out to me, so please humble me.
Thanks!
When working with typedef NS_ENUM
enums in swift you should omit enum name in comparison and assignment, here how you can compare it:
if ( testAppleConst == .Value0 ) { ... }
You can read more about this in Apple do s for Swift here.
这篇关于Swift:使用在Objective-C中声明的枚举,在Swift中麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!