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

前端 未结 9 600
感情败类
感情败类 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: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];
            }
        }
    

提交回复
热议问题