NSComparisonResult 行为异常
NSComparisonResult misbehaving
我的应用程序正在比较发送到函数的请求字符串,例如 -(void)sendSearchRequest:(NSString *)request sport:(NSString *)sport location:(NSString *)location
。我将请求设置为 'Kevin',将 运动设置为 'Baseball',将位置设置为 'Wellston'。
然后,我将它与请求为 'Kevin'、sport 为 'Basketball'、位置为 'Wellston' 的对象进行比较].这项运动应该 return 错误,但事实并非如此。怎么回事?
for (Athlete *athlete in self.athletes) {
NSComparisonResult result = [[athlete name] compare:request options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [request length])];
NSComparisonResult sportCompare = [[athlete sport] compare:sport options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [sport length])];
NSComparisonResult locationCompare = [location compare:self.cityName options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [self.cityName length])];
NSLog(@"Location: %@", location);
NSLog(@"Name: %@", request);
NSLog(@"Sport: %@", sport);
if (result == NSOrderedSame && sportCompare == NSOrderedSame && locationCompare == NSOrderedSame) {
[self.filteredArray addObject:athlete];
}
}
备注:
* 我必须考虑用户错误。这些变量是从 UITextField 传递的,因此如果用户输入 'Wellston, OK' 但对象的城市是 'Wellston',它将不起作用
如果你想知道两个字符串是否相同,为什么要使用NSComparisonResult?那根本没有意义。它用于排序。只需使用 isEqualToString:
- 这就是它的用途。
或者对于更复杂的相等性概念,您可以实施锚定子字符串搜索。您可以使它不区分大小写和不区分变音符号。因此,您可以确定一个字符串是否以另一个字符串开头,例如。
我的应用程序正在比较发送到函数的请求字符串,例如 -(void)sendSearchRequest:(NSString *)request sport:(NSString *)sport location:(NSString *)location
。我将请求设置为 'Kevin',将 运动设置为 'Baseball',将位置设置为 'Wellston'。
然后,我将它与请求为 'Kevin'、sport 为 'Basketball'、位置为 'Wellston' 的对象进行比较].这项运动应该 return 错误,但事实并非如此。怎么回事?
for (Athlete *athlete in self.athletes) {
NSComparisonResult result = [[athlete name] compare:request options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [request length])];
NSComparisonResult sportCompare = [[athlete sport] compare:sport options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [sport length])];
NSComparisonResult locationCompare = [location compare:self.cityName options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [self.cityName length])];
NSLog(@"Location: %@", location);
NSLog(@"Name: %@", request);
NSLog(@"Sport: %@", sport);
if (result == NSOrderedSame && sportCompare == NSOrderedSame && locationCompare == NSOrderedSame) {
[self.filteredArray addObject:athlete];
}
}
备注:
* 我必须考虑用户错误。这些变量是从 UITextField 传递的,因此如果用户输入 'Wellston, OK' 但对象的城市是 'Wellston',它将不起作用
如果你想知道两个字符串是否相同,为什么要使用NSComparisonResult?那根本没有意义。它用于排序。只需使用 isEqualToString:
- 这就是它的用途。
或者对于更复杂的相等性概念,您可以实施锚定子字符串搜索。您可以使它不区分大小写和不区分变音符号。因此,您可以确定一个字符串是否以另一个字符串开头,例如。