Comparing version numbers

后端 未结 8 1588
予麋鹿
予麋鹿 2020-12-11 11:39

Some time ago, I read that comparing version numbers can be done using the following code snippet:

NSString *vesrion_1 = @\"1.2.1\";
NSString *version_2 = @\         


        
8条回答
  •  遥遥无期
    2020-12-11 12:07

    Using NSNumericSearch works in most cases but it fails when version formats are different;

    e.g. when comparing

    [self compareBundleVersion:@"1.1.12" withBundleVersion:@"1.2"];
    

    it will return NSOrderedDescending while it should return NSOrderedAscending.

    Please have a look at my NSString category compareToVersion which handles this in a nice way;

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

    There are also a few helpers;

    [@"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"];
    

    Check it out at; https://github.com/stijnster/NSString-compareToVersion

提交回复
热议问题