【问题标题】:warning on typedef enum when converting app to 64-bit将应用程序转换为 64 位时对 typedef 枚举发出警告
【发布时间】:2014-02-21 07:00:57
【问题描述】:

我正在将我的 iOS 应用程序转换为 64 位。我安装了最新的 Xcode 5.1(beta 4)。

当我编译应用程序时,我收到了 100 多个警告,其中大部分都很容易修复。但是,我对以下代码有警告:

+ (CommentResponseStatus)commentReponseStatusCodeWithStatusString:(NSString *)_status
{
    NSArray *commentStatusString = [NSArray arrayWithObjects:@"success", @"needConfirmation", @"stopped", nil];

    return [commentStatusString indexOfObject:_status];
}

CommentResponseStatus 声明为:

typedef enum {
    success,
    needConfirmation,
    stopped
} CommentResponseStatus;

我有一个警告“隐式转换失去整数精度:'NSUInteger'(又名'unsigned long')到'CommentResponseStatus'”

警告上线return [commentStatusString indexOfObject:_status];

NSArray 我们有- (NSUInteger)indexOfObject:(id)anObject;

我对这个警告感到困惑,现在不知道如何解决它。任何快速帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c enums 64-bit


    【解决方案1】:

    根据apple docs 大约64位的变化。

    Enumerations Are Also Typed :在 LLVM 编译器中,枚举类型可以 定义枚举的大小。这意味着一些枚举 types 的大小也可能比您预期的要大。这 与所有其他情况一样,解决方案是不对 数据类型的大小。相反,将任何枚举值分配给变量 具有正确的数据类型

    为了解决这个问题,使用类型创建枚举,语法如下。

    typedef NS_ENUM(NSUInteger, CommentResponseStatus) {
        success,
        needConfirmation,
        stopped
    };
    

    typedef enum CommentResponseStatus : NSUInteger {
        success,
        needConfirmation,
        stopped
    } CommentResponseStatus;
    

    【讨论】:

    • 我尝试了第二种解决方案,警告消失了。不过我不确定第一个。我做了更多搜索并在这里找到了类似的答案:stackoverflow.com/questions/707512/…
    • 我个人推荐第一个,因为它只有一个地方可以重构改名。后者有两个,我也不喜欢后缀命名风格而不是后者的前缀。
    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2011-10-27
    相关资源
    最近更新 更多