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 模式代码优化导致的结果

  • 相关阅读:
    GitHub 更新fork的代码
    robotframework出现错误:Keyword 'AppiumLibrary.Open Application' expected 1 to 2 non-keyword arguments,got 5.
    adb命令积累
    appium测试android环境搭建(win7)
    小明的自留地
    转载:appium踩过的坑
    junit3和junit4的使用区别如下
    Python线程指南
    实现ie低版本支持input type="number"
    LODOP打印开发
  • 原文地址:https://www.cnblogs.com/xzjxylophone/p/6553179.html
Copyright © 2011-2022 走看看