美文网首页
iOS自定义版本号比较

iOS自定义版本号比较

作者: iOS小乔 | 来源:发表于2017-04-14 08:58 被阅读120次

    **代码如下,写的还比较粗糙 **

     -(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;
    }

    相关文章

      网友评论

          本文标题:iOS自定义版本号比较

          本文链接:https://www.haomeiwen.com/subject/zhprgttx.html