**代码如下,写的还比较粗糙 **
-(int)compareVersion1:(NSString *)version1 version2:(NSString *)version2{
if ([version1 isEqualToString:version2]) {
return 0;
}
NSArray *version1Arr = [version1 componentsSeparatedByString:@"."];
NSArray *version2Arr = [version2 componentsSeparatedByString:@"."];
NSMutableArray *mu1Arr = [NSMutableArray arrayWithArray:version1Arr];
NSMutableArray *mu2Arr = [NSMutableArray arrayWithArray:version2Arr];
for (NSInteger i = mu1Arr.count; i<3; i++) {
[mu1Arr addObject:@"0"];
}
for (NSInteger i = mu2Arr.count; i<3; i++) {
[mu2Arr addObject:@"0"];
}
for (int i=0; i<mu1Arr.count; i++) {
NSInteger v1 = [mu1Arr[i] integerValue];
NSInteger v2 = [mu2Arr[i] integerValue];
if (v1!=v2) {
return v1>v2?1:-1;
}
}
return 0;
}
网友评论