代码改变世界

iOS App版本号compare

2018-11-16 10:37  法子  阅读(338)  评论(0编辑  收藏  举报

参考原文:https://www.jianshu.com/p/1dd7745f5ee4

NSStringCompareOptions里有个NSNumericSearch

 /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */

Object-C

    NSString *currentVersion = @"1.2.1";
    NSString *serverVersion = @"1.11.1";
    if ([currentVersion compare:serverVersion options:NSNumericSearch] == NSOrderedAscending) {
        NSLog(@"需要更新");
    }

Swift

        let currentVersion = "1.2.1"
        let serverVersion = "1.11.1"
        if currentVersion.compare(serverVersion, options: .numeric) == .orderedAscending {
            print("需要更新")
        }