IOS 中获取系统版本,比较简单([[UIDevice currentDevice] systemVersion]);
看到网络上很多转化系统字符串到float 的方法,都是使用 [[[UIDevice currentDevice] systemVersion] floatValue];
但是这个方法我遇到一个问题,就是如果系统版本是7.1.1 的时候,转化生成的float 有问题,我debug 生成的数据为7.0899999999999。导致出现了问题,
后来为了解决这个问题,我自己写了一个方法。
+ (CGFloat )systemVersion{
NSString *sVersion = [[UIDevice currentDevice] systemVersion];
NSRange fRange = [sVersion rangeOfString:@"."];
CGFloat version = 0.0f;
if(fRange.location != NSNotFound){
sVersion = [sVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
NSMutableString *mVersion = [NSMutableString stringWithString:sVersion];
[mVersion insertString:@"." atIndex:fRange.location];
version = [mVersion floatValue];
}else {
// 版本应该有问题(由于ios 的版本 是7.0.1,没有发现出现过没有小数点的情况)
version = [sVersion floatValue];
}
return version;
}
本文介绍了一种解决iOS系统版本字符串转换为Float时遇到的问题的方法。当系统版本如7.1.1时,直接使用floatValue会得到错误的值7.0899999999999。为了解决这个问题,作者提供了一个自定义方法,通过查找小数点并重新构建字符串来准确地获取系统版本的float值。
1505

被折叠的 条评论
为什么被折叠?



