iOS 字符串的的compare方法

比较app版本号, 决定是否更新

后台存储的版本号1.0.4

当前的版本号 1.0.1

- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;

    NSString *serverVersion = @"1.0.4"; // 后台存储的版本号
    NSString *curVersion = @"1.0.1";    // 当前app的版本号
    
    NSComparisonResult comResult = [serverVersion compare:curVersion options:NSNumericSearch];
    
    if (comResult == NSOrderedDescending) {
        
        // serverVersion > curVersion
        // 提示app进行升级
        
    }
    else if (comResult == NSOrderedAscending) {
        
        // serverVersion < curVersion
        
    }
    else if (comResult == NSOrderedSame) {
        
        // serverVersion = curVersion
    }

枚举值:

options:(NSStringCompareOptions)

typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {

    NSCaseInsensitiveSearch = 1, /**/

    NSLiteralSearch = 2, /* 按字符等价的精确字符, 区分大小写*/

    NSBackwardsSearch = 4, /* Search from end of source string */

    NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */

    NSNumericSearch = 64, /*数字比较  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 */

    NSDiacriticInsensitiveSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 128, /* If specified, ignores diacritics (o-umlaut == o) */

    NSWidthInsensitiveSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 256, /* If specified, ignores width differences ('a' == UFF41) */

    NSForcedOrderingSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 512, /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */

    NSRegularExpressionSearch API_AVAILABLE(macos(10.7), ios(3.2), watchos(2.0), tvos(9.0)) = 1024    /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值