[英]Using predicates to search nested arrays in iOS 5
I have an array of Address Book contact dictionaries, with the dictionary for each name containing a string for the name and an array of email addresses. 我有一组地址簿联系词典,每个名称的字典包含名称的字符串和电子邮件地址数组。 Here's a snippet of what the NSLog output looks like when I log the array of contacts:
这是我记录联系人数组时NSLog输出的代码片段:
{
emails = (
"something@yahoo.com"
);
name = "Some Name";
},
{
emails = (
"john.public@gmail.com",
"john@public.name"
);
name = "John Q. Public";
},
[etc.]
I want to use a predicate to search these dictionaries by email address, returning any and all entries that have at least one email address that matches the search term. 我想使用谓词通过电子邮件地址搜索这些词典,返回至少有一个与搜索词匹配的电子邮件地址的所有条目。
So far, I have tried the method described in this question , just using CONTAINS, like so: 到目前为止,我已经尝试了这个问题中描述的方法,只使用CONTAINS,如下所示:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"emails CONTAINS[c] %@", searchString];
but any search just returned an empty array. 但任何搜索只返回一个空数组。 If I search the name field instead, like so, it works fine:
如果我搜索名称字段,就像这样,它工作正常:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[c] %@", searchString];
So I'm pretty sure it's specifically with searching the array. 所以我很确定它专门用于搜索数组。 Ideas?
想法?
Aaand I figured it out. Aaand我弄清楚了。 This works:
这有效:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY emails CONTAINS[c] %@",currentString];
Thanks for being my rubber ducky, SO. 谢谢你是我的橡皮鸭,所以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.