zoukankan      html  css  js  c++  java
  • iOS enum C方法 DEBUG, RELEASE的隐藏的一个坑

    开发了一个app, 在debug模式下没有任何问题,在release模式下就直接崩溃.

    经过一段时间的定位终于定位到如下的这一段代码:

        
    E_BZ_TestType type = [dic[@"type"] integerValue];
       // 调用代码 self.sourceLabel.text
    = [NSString stringWithFormat:@"%@", NSStringFromE_BZ_TestType(type)];

    其中枚举定义和枚举翻译中文定义如下:

    // 枚举  在一个 XXDefine.h中
    typedef NS_ENUM(NSInteger, E_BZ_TestType) {
        kE_BZ_TestType_None     =       0,
        kE_BZ_TestType_One      =       1,
    };
    // 翻译函数定义
    NSString *NSStringFromE_BZ_TestType(E_BZ_TestType type);
    
    
    // 翻译函数实现  在一个 XXDefine.m中
    NSString *NSStringFromE_BZ_TestType(E_BZ_TestType type)
    {
        switch (type) {
            case kE_BZ_TestType_None:
                return @"没有";
            case kE_BZ_TestType_One:
                return @"";
            default:
                break;
        }
    }

    注意上面的代码:

    1. 代码不在任何类内部

    2. 翻译函数的  default  是break, 没有返回任何值.

    3. 在debug/Release 模式下, 翻译函数没有产生任何错误和任何警告

    对于注意 第3条 如果把翻译函数实现改成如下就会出现错误

    NSString *NSStringFromE_BZ_TestType(E_BZ_TestType type)
    {
        switch (type) {
            case kE_BZ_TestType_None:
                return @"没有";
            default:
                break;
        }
    }

    那么现在 问题来了:

    当在调用代码中 type 不属于枚举中的任何一个的时候,例如type = 100

    在使用翻译函数的时候:

    DEBUG 模式是: 返回了100字符串

    而在 RELEASE模式是 直接崩溃了

    所以只能把翻译函数修改如下:

    NSString *NSStringFromE_BZ_TestType(E_BZ_TestType type)
    {
        switch (type) {
            case kE_BZ_TestType_None:
                return @"没有";
            case kE_BZ_TestType_One:
                return @"";
            default:
                return @"未知";
        }
    }

    这样 就不会出现问题了.

    我想这是 Release 模式代码优化导致的结果

  • 相关阅读:
    Eclipse '《》'operator is not allowed for source level below 1.7
    eclipse怎么在包里建一个包
    java jdk environment variables
    甲骨文中国 / Oracle 提供的技术资源
    在Eclipse中使用log4j配置实例听
    StringBuilder的常用方法
    mediasoup-demo公网安装部署
    使用NodeJs新建一个https服务器,并可以发布静态资源
    使用shell脚本批量执行adb命令,卸载安装apk
    这 5 个前端组件库,可以让你放弃 jQuery UI
  • 原文地址:https://www.cnblogs.com/xzjxylophone/p/6553179.html
Copyright © 2011-2022 走看看