How to use compare on a version number where theres less parts in one number in Objective-C?

前端 未结 9 598
感情败类
感情败类 2021-01-22 02:30

I\'ve found the following code at http://snipplr.com/view/2771

Which is pretty good, almost exactly what I was looking for, but if I use the values @\"1.4.5\", @

相关标签:
9条回答
  • 2021-01-22 02:41

    The Sparkle framework for Mac is open source, and it has some neat version checking code you can have a look at: https://github.com/andymatuschak/Sparkle/blob/master/SUStandardVersionComparator.m

    0 讨论(0)
  • 2021-01-22 02:43

    Why don't use NSString compare:options:NSNumericSearch

    NSString *sysVer = [[UIDevice currentDevice] systemVersion];
    NSLog(@"%@,%d,%d,%d", sysVer, [sysVer compare:@"1.0" options: NSNumericSearch], [sysVer compare:@"6.0" options: NSNumericSearch],[sysVer compare:@"10.0" options: NSNumericSearch]);
    if ([sysVer compare:@"6.0" options: NSNumericSearch]>=NSOrderedSame) {
        NSLog(@"ios 6");
    }
    
    0 讨论(0)
  • 2021-01-22 02:43

    As answered in this post; Compare version numbers in Objective-C

    Check out my NSString category that implements easy version checking on github; https://github.com/stijnster/NSString-compareToVersion

    [@"1.2.2.4" compareToVersion:@"1.2.2.5"];
    

    This will return a NSComparisonResult which is more accurate then using;

    [@"1.2.2" compare:@"1.2.2.5" options:NSNumericSearch]
    

    Helpers are also added;

    [@"1.2.2.4" isOlderThanVersion:@"1.2.2.5"];
    [@"1.2.2.4" isNewerThanVersion:@"1.2.2.5"];
    [@"1.2.2.4" isEqualToVersion:@"1.2.2.5"];
    [@"1.2.2.4" isEqualOrOlderThanVersion:@"1.2.2.5"];
    [@"1.2.2.4" isEqualOrNewerThanVersion:@"1.2.2.5"];
    
    0 讨论(0)
  • 2021-01-22 02:47

    You can use my Version class that helps you to parse your version strings into Version objects for easy compare. It supports 4 field version numbers like major.minor.release.build, all fields are optional. Also, it has a compare method for comapring two version objects.

    https://github.com/polatolu/version-ios

    0 讨论(0)
  • 2021-01-22 02:49

    My sense would be that the first numeral group is always the most significant, so 10.anything is bigger than 9.anything.anything. If I'm right about that, then the solution is to replace the dots with zeros and pad the shorter string on the right-hand side with zeros to match the length of the longer string:

    e.g.
    9.4   --->  90400  (padded on the right with 00)
    8.6.7 --->  80607
    

    What's nice about this is, that if I'm wrong about the requirement, the algorithm can be readily fixed by padding the shorter string on the right.

    - (NSComparisonResult)compareVersion:(NSString *)vA withVersion:(NSString *)vB {
    
        NSString *vAPadded = [vA stringByReplacingOccurrencesOfString:@"." withString:@"0"];
        NSString *vBPadded = [vB stringByReplacingOccurrencesOfString:@"." withString:@"0"];
    
        while (vAPadded.length < vBPadded.length)
            vAPadded = [vAPadded stringByAppendingString:@"0"];
    
        while (vBPadded.length < vAPadded.length)
            vBPadded = [vBPadded stringByAppendingString:@"0"];
    
        return [vAPadded intValue] - [vBPadded intValue];
    }
    

    If I've got the requirement backwards on significant digits, change the pads like this:

    vAPadded = [@"0" stringByAppendingString:vAPadded];
    
    0 讨论(0)
  • 2021-01-22 02:51

    so you want to compare 10.5 to 1.4.6 such that 10.5 is viewed as 0.10.5

    if this is the case, you need to add "0" array items to the left side of your separated version number

    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 insertObject:@"0" atIndex:0];
            }
        } else if ([leftFields count] > [rightFields count]) {
            while ([leftFields count] != [rightFields count]) {
                [rightFields insertObject:@"0" atIndex:0];
            }
        }
    
    0 讨论(0)
提交回复
热议问题