iOS 基于 Cotains 的谓词搜索仅在找到完全匹配时返回
我正在将一个搜索栏添加到我的 iOS 应用程序的表视图组件中,该组件允许搜索由字典组成的 NSArray。不过,搜索栏并不总是有效 - 如果我的数据完全匹配,但不是部分匹配,它会成功打印出结果。例如,如果输入了确切的短语,它会打印出“Turn off a light”作为结果,但如果只输入“Turn”则不会。然而,在仅由字符串组成的调试数组上,搜索功能完全按照预期工作。
我的谓词代码在这里:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchText];
self.searchResults = [allSearchableItems filteredArrayUsingPredicate:resultPredicate];
}
实际工作的测试块是这样写的:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchText];
NSArray *array = [NSArray arrayWithObjects:@"Miguel", @"Ben", @"Adam", @"Melissa", nil];
NSArray *moreStuff = [array filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@", moreStuff);
self.searchResults = [allSearchableItems filteredArrayUsingPredicate:resultPredicate];
}
在上面的代码块中,NSArray“moreStuff”打印出来并正确更新,但是在第一个块中,NSArray“searchResults”仅在以下情况下返回结果:输入精确匹配。我在这里完全不知所措,所以任何帮助/建议将非常感激。
I'm adding a search bar to a table view component of my iOS application that allows searching through an NSArray comprised of Dictionaries. The search bar does not work all the time, though - it successfully prints out a result if there is an exact match for my data, but not a partial match. For instance, it will print out "Turn off a light" as a result if that exact phrase is entered, but not if just "Turn" is entered. On a debugging array made only of strings, however, the search functionality worked exactly as expected.
My predicate code is here:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchText];
self.searchResults = [allSearchableItems filteredArrayUsingPredicate:resultPredicate];
}
The test block that actually worked was written like this:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchText];
NSArray *array = [NSArray arrayWithObjects:@"Miguel", @"Ben", @"Adam", @"Melissa", nil];
NSArray *moreStuff = [array filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@", moreStuff);
self.searchResults = [allSearchableItems filteredArrayUsingPredicate:resultPredicate];
}
In the above block of code, the NSArray "moreStuff" printed out and updated correctly, however in the first block the NSArray "searchResults" only returns a result when an exact match is entered. I'm at a complete loss here, so any help/recommendations will be super appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您正在对象中搜索字符串,所以我认为您只需要在谓词中深入挖掘即可。对象内的字符串只是该对象的部分匹配。当您在谓词中使用 SELF 时,您指定在对象本身内进行搜索,在您的例子中是一个 NSDictionary。 NSDictionary 中的字符串字段本质上是对象中的对象。因此,如果您要在字典中查找名为“Name”的字段,则不应在 SELF 中查找谓词,而应在 SELF.Name 中查找。您可以尝试以下代码来搜索 NSDictionary 的 Name 字段。
如果您仍然需要检查对象中包含的其他字段,您可以创建复合谓词。有关谓词的详细信息,请参阅 Apple 的谓词编程指南。
我希望这会有所帮助!
Because you are searching for a string in an object, I think you just need to dig a bit deeper in your predicate. A string within the object is only a partial match of that object. When you use SELF in the predicate, you're specifying to search within the object itself, which in your case is an NSDictionary. String fields in the NSDictionary are essentially objects within an object. So if you are looking for a field called 'Name' in your Dictionary, instead of having the predicate look in SELF, it should look in SELF.Name. You could try the following code to search the Name field of your NSDictionary.
If you still need to check other fields contained within the object, you can create a compound predicate. Details on Predicates can be found in Apple's Predicate Programming Guide.
I hope this helps!