iOS 字符串的的compare方法

xiaoxiao2025-06-04  46

比较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 */

};

转载请注明原文地址: https://www.6miu.com/read-5031275.html

最新回复(0)