tags:

views:

49

answers:

1

I have a model class that has "name" and "description". I would to perform a search using valueForKeyPath.

For example, using loop I would perform the search this way:

for (Model model in allModels) {

if (mode.name == "name" || [absoluteURL rangeOfString:@"my_substring"].location == NSNotFound ) {
   // put this model in a result list
}

}

Now, how do I do the same with something like this?

NSArray *result = [allModels valueForKeyPath: @"[collect].{name == @"name"}”];

+2  A: 

You can use NSPredicate for this (simplified example below). You can add multiple predicates to achieve what you'd like.

NSMutableArray *matchingItems = [NSMutableArray array];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"mode.name is[cd] %@", @"name"];

for (Model *model in allModels) {
    if ([pred evaluateWithObject:model]) {
        [matchingItems addObject:model];
    }
}
iKenndac