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

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

提交回复
热议问题