Using NSPredicate to search through array of objects

Viewed 11024

I have an array of instances of a class called Contact, which has, among others, the following properties:

NSArray *mailAddressList // Array of NSString
NSArray *websiteList // Array of NSString
NSArray *tags // Array of instances of Tag class

The class tag has the following properties:

NSString *name;
UIColor *color;

I want to use NSPredicate to search a string in any property of each Contact. This is the code I have:

if([scope isEqualToString:SCOPE_MAIL] || [scope isEqualToString:SCOPE_WEBSITE])
{
    // Search through an array
    predicate = [NSPredicate predicateWithFormat:@"ANY SELF.%@ contains[c] %@", scope, textSearch];
}
else if([scope isEqualToString:SCOPE_TAG])
{
    // Search another object's property
    predicate = [NSPredicate predicateWithFormat:@"SELF.%@.name contains[c] %@", scope, textSearch];
}
else
{
    // The rest of the properties are instances of NSString
    predicate = [NSPredicate predicateWithFormat:@"SELF.%@ contains[c] %@", scope, textSearch];
}

Everything works fine except for SCOPE_TAG, it doesn't return any values. I don't think I'm using the predicate correctly.

NOTE: I'm new with NSPredicate so I would like to hear some insights if what I'm doing is not ok

1 Answers
Related