Its my code :
-(NSArray *)searchTeamsWithPlayerPharse:(NSMutableArray *)teams phrase:(NSString *)phrase
{
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"rel_Players.fullName CONTAINS[c] %@" ,phrase];
NSArray *searchResult = [teams filteredArrayUsingPredicate:searchPredicate];
return searchResult ;
}
It is not working. In array I have Team objects. Team has relationship with players and i want filtr with this relathionship and find only teams with players have pharse in fullName. How to change this NSPredicate
The ANY
operator should work:
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"ANY rel_Players.fullName CONTAINS[c] %@" ,phrase];
Some additional remarks:
rel_
prefix is unusual and unnecessary: the plural form players is enough and the underscore can be omitted because the beginning of a new word is marked by an uppercase character (camel case).NSMutableArray
. If you change the parameter type to NSArray *
you can use instances of both NSArray
and NSMutableArray
.self
. You can make a function out of it or – I would prefer that – make it a method of NSArray
.Taking this together:
@interface NSArray(TeamPlayerAddition)
-(NSArray *)teamsWithPlayerPharse:(NSString *)phrase
@end
@implementation NSArray(TeamPlayerAddition)
-(NSArray *)teamsWithPlayerPharse:(NSString *)phrase
{
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"ANY rel_Players.fullName CONTAINS[c] %@" ,phrase];
return [self filteredArrayUsingPredicate:searchPredicate];
}
@end