Search code examples
objective-cnsarraynspredicate

NSPredicate filter array


How can I use NSPredicate to filter an array where the object in the array's ->_title variable is equal to the variable title? I tried the following below, but it doesn't filter anything out.

NSMutableArray *array = [[posts mutableCopy] autorelease];
NSString *title = [[TBForrstr sharedForrstr] stringForPostType:type];
[array filterUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"SELF->_title == %@", title]]];

Solution

  • Make sure the title matches exactly in the array. Or, if you want to be more flexible, make it case-insensitive:

    NSMutableArray *array = [[posts mutableCopy] autorelease];
    NSString *title = [[TBForrstr sharedForrstr] stringForPostType:type];
    [array filterUsingPredicate:[NSPredicate predicateWithFormat:@"_title == [cd]%@", title]];