如何在Objective-C中使用一个数字中较少部分的版本号进行比较? [英] How to use compare on a version number where theres less parts in one number in Objective-C?

查看:94
本文介绍了如何在Objective-C中使用一个数字中较少部分的版本号进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://snipplr.com/view/2771 上找到了以下代码

这是非常好的,几乎正是我想要的,但如果我使用值 @1.4.5,@10.4 它产生错误的结果,说第一个数字较低。

Which is pretty good, almost exactly what I was looking for, but if I use the values @"1.4.5", @"10.4" it produces the wrong result, saying that the first number is lower.

Arghhhh深夜编码,对不起我读了10.4作为1.4 :(

我不确定为什么比较有问题,问题是什么?

I'm unsure why compare is having an issue and what the problem is ?

/*
 * compareVersions(@"10.4",             @"10.3"); //             
       returns NSOrderedDescending (1) - aka first number is higher

 * compareVersions(@"10.5",             @"10.5.0"); //           
       returns NSOrderedSame (0) 

 * compareVersions(@"10.4 Build 8L127", @"10.4 Build 8P135"); // 
       returns NSOrderedAscending (-1) - aka first number is lower
 */
NSComparisonResult compareVersions(NSString* leftVersion, NSString* rightVersion)
{
    int i;

    // Break version into fields (separated by '.')
    NSMutableArray *leftFields  = [[NSMutableArray alloc] initWithArray:[leftVersion  componentsSeparatedByString:@"."]];
    NSMutableArray *rightFields = [[NSMutableArray alloc] initWithArray:[rightVersion componentsSeparatedByString:@"."]];

    // Implict ".0" in case version doesn't have the same number of '.'
    if ([leftFields count] < [rightFields count]) {
        while ([leftFields count] != [rightFields count]) {
            [leftFields addObject:@"0"];
        }
    } else if ([leftFields count] > [rightFields count]) {
        while ([leftFields count] != [rightFields count]) {
            [rightFields addObject:@"0"];
        }
    }

    // Do a numeric comparison on each field
    for(i = 0; i < [leftFields count]; i++) {
        NSComparisonResult result = [[leftFields objectAtIndex:i] compare:[rightFields objectAtIndex:i] options:NSNumericSearch];
        if (result != NSOrderedSame) {
            [leftFields release];
            [rightFields release];
            return result;
        }
    }

    [leftFields release];
    [rightFields release];  
    return NSOrderedSame;
}


推荐答案

[我今天早些时候发布了这个,但它没有被选为答案,它可能更适合您的问题。还有其他技术,你可以看这里此处用于其他解决方案。]

[I posted this earlier today, but it was not selected as the answer, and it may be more appropriate to your problem. There are other techniques, you can look here and here for other solutions.]

我所做的就是将该字符串分解为组件:

What I do is take that string and break it into components:

NSArray *array = [myVersion componentsSeparatedByCharactersInSet:@"."];

NSInteger value = 0;
NSInteger multiplier = 1000000;
for(NSString *n in array) {
  value += [n integerValue] * multiplier;
  multiplier /= 100;
}

这样做可以为您提供可用于比较的标准化值,以及通常会比较具有不同深度的版本,即1.5和1.5.2。

What this does is give you a normalized value you can use for comparison, and will generally compare releases that have different "depths", ie 1.5 and 1.5.2.

如果您的点数超过100(即任何数字> 100),它会中断,并且还会声明1.5.0 == 1.5。也就是说,它简短,甜美,易于使用。

It breaks if you have more than 100 point releases (ie any number is > 100) and also will declare 1.5.0 == 1.5. That said, its short, sweet, and simple to use.

编辑:如果你使用NSString'compare:options:'方法,请确保你的字符串很好修饰:

if you use the NSString 'compare:options:' method, make sure you have your string well groomed:

    s1 = @"1.";
    s2 = @"1";
    NSLog(@"Compare %@ to %@ result %d", s1, s2, (int)[s1 compare:s2 options:NSNumericSearch]);
    s1 = @"20.20.0";
    s2 = @"20.20";
    NSLog(@"Compare %@ to %@ result %d", s1, s2, (int)[s1 compare:s2 options:NSNumericSearch]);

2012-09-06 11:26:24.793 xxx[59804:f803] Compare 1. to 1 result 1
2012-09-06 11:26:24.794 xxx[59804:f803] Compare 20.20.0 to 20.20 result 1

这篇关于如何在Objective-C中使用一个数字中较少部分的版本号进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆