gpt4 book ai didi

ios - NSComparisonResult 行为异常

转载 作者:行者123 更新时间:2023-11-29 10:33:39 25 4
gpt4 key购买 nike

我的应用程序正在比较发送到函数的请求字符串,例如 -(void)sendSearchRequest:(NSString *)request sport:(NSString *)sport location:(NSString *)location。我将请求设置为“Kevin”,运动设置为“棒球”,位置设置为“Wellston”。

然后,我将它与请求为“Kevin”、运动ort 为“Basketball”、位置为“Wellston”的对象进行比较。运动应该返回 false,但事实并非如此。怎么回事?

   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: - 这就是它的用途。

或者对于更复杂的相等性概念,您可以实现锚定子字符串搜索。您可以使它不区分大小写和不区分变音符号。因此,例如,您可以确定一个字符串是否以另一个字符串开头。

关于ios - NSComparisonResult 行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com