NSLineBreakMode enum and backwards compatibility

房东的猫 提交于 2019-12-22 14:08:43

问题


I need my app to support iOS 5+. Since prior iOS 6 the enum lineBreakMode for line break mode in UILabel is of type UILineBreakMode, and it is of type NSLineBreakMode for iOS 6+, what should be the best (or more correct) way to check the iOS version currently running to determine the type to be used? Is it correct to directly do something like [[UIDevice currentDevice] systemVersion], or is there a better way?

Thanks!


回答1:


You do not need to check the iOS version at runtime, the enum values are the same, the compiled code will not be changed when moving from UILineBreakMode to NSLineBreakMode

enum {

NSLineBreakByWordWrapping = 0,
   NSLineBreakByCharWrapping,
   NSLineBreakByClipping,
   NSLineBreakByTruncatingHead,
   NSLineBreakByTruncatingTail,
   NSLineBreakByTruncatingMiddle
};
typedef NSUInteger NSLineBreakMode


typedef enum {
   UILineBreakModeWordWrap = 0,
   UILineBreakModeCharacterWrap,
   UILineBreakModeClip,
   UILineBreakModeHeadTruncation,
   UILineBreakModeTailTruncation,
   UILineBreakModeMiddleTruncation,
} UILineBreakMode;



回答2:


In case you want to check for the OS version you can use this code:

+ (NSInteger)OSVersion
{
    static NSUInteger _deviceSystemMajorVersion = -1;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _deviceSystemMajorVersion = [[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue];
    });
    return _deviceSystemMajorVersion;
}


来源:https://stackoverflow.com/questions/19652424/nslinebreakmode-enum-and-backwards-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!