我想比较两个字符串
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];
}
所以我对结果感到很困惑? (数量,类等)
答案 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...
}
}