此代码可以比较一"."分割的版本好
//只有当newVersion大于oddVersion是才返回true 如2.1.0和2.0.0的比较
- (BOOL)cmpNewVersion:(NSString *)newVersion oldVersion:(NSString *)oddVersion{
NSArray *newVs = [newVersion componentsSeparatedByString:@"."];
NSArray *oldVs = [oddVersion componentsSeparatedByString:@"."];
NSInteger cmpCount = oldVs.count;
if (newVs.count < oldVs.count) {
cmpCount = newVs.count;
}
for (int i = 0; i<cmpCount; i++) {
int nv = [newVs[i] intValue];
int ov = [oldVs[i] intValue];
//NSLog(@"ov1=%@,nv1=%@,i=%@",@(ov),@(nv),@(i));
if (nv!=ov) {
if (nv>ov) {
return YES;
}else{
return NO;
}
}
}
return NO;
}
网友评论