比较版本号
1.1 = 1.1.0Bata
1.0.0.1 = 1...1
1.2 > 1.1.3
2 > 1.9
2.0.0.10 < 2.0.1
/**
比较版本号
1.1 = 1.1.0Bata
1.0.0.1 = 1...1
1.2 > 1.1.3
2 > 1.9
2.0.0.10 < 2.0.1
@param v1 版本1
@param v2 版本2
@return 返回0:相等 1:v1>v2 -1:v1<v2
*/
+ (int)convertVersion:(NSString *)v1 v2:(NSString *)v2
{
// 去除杂质,只留下数字和点
NSString *v1_n = [[v1 componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet]] componentsJoinedByString:@""];
NSString *v2_n = [[v2 componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet]] componentsJoinedByString:@""];
// 分解成数组
NSArray *v1_arr = [v1_n componentsSeparatedByString:@"."];
NSArray *v2_arr = [v2_n componentsSeparatedByString:@"."];
// 取数组最大值
NSInteger count = MAX(v1_arr.count, v2_arr.count);
for (NSInteger i = 0; i < count; i++) {
NSInteger v1_i = 0;
NSInteger v2_i = 0;
if (v1_arr.count > i) {
v1_i = [v1_arr[i] integerValue];
}
if (v2_arr.count > i) {
v2_i = [v2_arr[i] integerValue];
}
// 按顺序比较大小
if (v1_i != v2_i) {
return v1_i>v2_i?1:-1;
}
}
// 循环结束,返回相等
return 0;
}