简体   繁体   中英

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:

{
    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:

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. This works:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY emails CONTAINS[c] %@",currentString];

Thanks for being my rubber ducky, SO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM