Ios NSSTRING与NSDIACRITICIN敏感性研究的比较;
我试图比较两个字符串Ios NSSTRING与NSDIACRITICIN敏感性研究的比较;,ios,ios4,nsstring,string-comparison,Ios,Ios4,Nsstring,String Comparison,我试图比较两个字符串 String1来自一个文件 String2位于NSArray(预定义列表)中 我想比较这两个字符串,如果匹配,可以执行NSLog NSStringCompareOptions compareOptions = NSDiacriticInsensitiveSearch; NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha",
- 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];
}
所以我很困惑结果是什么?(编号、等级等)查看可用的Apple文档 如果搜索“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...
}
}
谢谢什么是Deredascending?我强烈建议您阅读苹果公司关于与NSString进行比较的文档。