iOS-版本号比对

2019-02-18  本文已影响1人  和谐共处

此代码可以比较一"."分割的版本好

//只有当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;
    
}
上一篇下一篇

猜你喜欢

热点阅读