使用NSDiacriticInsensitiveSearch的NSStringCompareOptions;

时间:2011-05-11 17:52:50

标签: ios ios4 nsstring string-comparison

我想比较两个字符串

  • String1来自文件。
  • String2位于NSArray(预定义列表)

我想比较两个字符串,如果匹配,可以做一个NSLog

NSStringCompareOptions compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha",
                                                         @"beta",
                                                         @"gamma",
                                                         nil];

for (NSString* element in countryIndex) {
    NSComparisonResult result = [(NSString *)country compare:element options:compareOptions];
}

所以我对结果感到很困惑? (数量,类等)

1 个答案:

答案 0 :(得分:2)

查看可用的Apple文档here

如果你搜索'NSComparisonResult',你会看到它是一个枚举,包含你可以用来检查比较操作产生的结果的常量。

以下是链接文档的简短摘录:

NSComparisonResult
These constants are used to indicate how items in a request are ordered.

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

因此,例如,为了在您的代码中使用它,您可以执行以下操作:

NSStringCompareOptions  compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha", @"beta", @"gamma' nil];

for (NSString* element in countryIndex) {
    NSInteger result = [(NSString *)country compare:element options:compareOptions];
    if(NSOrderedAscending == result) {
        // Do something here...
    }
    else if (NSOrderedSame == result) {
        // Do another thing here if they match...
    }
    else {
        // Try something else...
    }
}