加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:41:53 所属栏目:百科 来源:网络整理
导读:好.当我输入问题时查看了各种答案,并没有看到列出的答案. 这似乎是一个基本的,根本性的问题,我必须做错事,但我被驱使疯了(实际上,并不是一个“驱动器”.更多的是一个短推杆)试图找出我的错误. 我创建了一个测试项目that can be downloaded here(小项目). 无
好.当我输入问题时查看了各种答案,并没有看到列出的答案.

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

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

无论如何,我在处理导入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 );

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

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

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

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

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

谢谢!

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读