使用 NSPredicate 搜索 NSDictionaries 的 NSArray 时没有结果
嘿, 我有一个 NSDictionaries 数组(例如 [tempArray addObject:[[[NSMutableDictionary alloc] initWithObjects:someArray forKeys:keys] autorelease]];
),当我尝试使用 NSPredicate 搜索它时,它不断给出我“没有结果”。 (TempArray 变为 self.patentList)
我没有编译时警告或错误,如果我 NSLog([self.filteredArray count]);
那么它也会返回 0。
这是我的相关代码:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(first contains[cd] %@)", self.searchBar.text];
self.filteredArray = [self.patientList filteredArrayUsingPredicate:predicate];
return [self.filteredArray count];
self. PatientList 和 self.filteredArray 分别是 NSMutableArray 和 NSArray 类型(将 self.filteredArray 更改为 NSMutableArray 没有帮助)。
我也尝试过使用 == 而不是 contains 。使用 @"SELF contains[cd] %@" 仅在键入完整项目时返回一个项目(即,如果键“name”是@“Bob”,那么如果我输入 Bob,它将显示它,但当我输入 Bo 或 ob 时则不会)。
我真的被这个难住了。
提前致谢!
Hey,
I have an Array of NSDictionaries (e.g. [tempArray addObject:[[[NSMutableDictionary alloc] initWithObjects:someArray forKeys:keys] autorelease]];
) and when I try to search through it with NSPredicate it keeps giving me 'No Results'. (TempArray becomes self.patientList)
I have no compile-time warnings or errors, and if I NSLog([self.filteredArray count]);
then it returns 0 as well.
Here is my relevant code:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(first contains[cd] %@)", self.searchBar.text];
self.filteredArray = [self.patientList filteredArrayUsingPredicate:predicate];
return [self.filteredArray count];
self.patientList and self.filteredArray are types NSMutableArray and NSArray respectively (changing self.filteredArray to NSMutableArray doesn't help).
I have tried using == instead of contains as well. Using @"SELF contains[cd] %@"
only returns an item when the full item is typed (i.e. if the key 'name' is @"Bob" then if I type in Bob it will display it, but not when I type Bo or ob).
I'm really stumped on this one.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,first 是使用谓词时的保留字。来自苹果的文档:
只需将您的密钥更改为其他内容,也许是名字,它就会起作用。
Apparently first is a reserved word when using predicates. From Apple's documentation:
Just change your key to something else, firstName perhaps, and it will work.
您可以使用“like”来代替:
You can use "like" instead:
乍一看,这似乎是正确的。那么:你的字典实际上有一个名为
first
的键吗?如果确实如此,情况是否完全匹配?At first glance, it seems correct. So then: do your dictionaries actually have a key called
first
? And if they do, does it match the case exactly?