Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to filter NSMutableArray with NSPredicate and getting error
    text
    copied!<p>I'm trying to filter the results of a list of employee contacts that I keep in my app but am getting the following error : 'Can't use in/contains operator with collection LAST (not a collection)'</p> <p>I've tried several variations with the NSPredicate command, self, self.last, employee.last, last == 'smith' (this one doesn't generate an error but doesn't return any results). </p> <pre><code>NSMutableArray *employeesList = [[NSMutableArray alloc] init]; Person2 *employee = [[Person2 alloc] init]; employee.first = @"bob"; employee.last = @"black"; [employeesList addObject:employee]; employee = [[Person2 alloc] init]; employee.first = @"jack"; employee.last = @"brown"; [employeesList addObject:employee]; employee = [[Person2 alloc] init]; employee.first = @"george"; employee.last = @"smith"; [employeesList addObject:employee]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"last contains[cd] %@", @"black"]; NSArray *filteredKeys = [employeesList filteredArrayUsingPredicate:predicate]; NSLog(@"filtered : %@",filteredKeys); </code></pre> <p>[person2.h]</p> <pre><code>@interface Person2 : NSObject { @private } @property (nonatomic, retain) NSString *first; @property (nonatomic, retain) NSString *last; + (Person2 *)personWithFirst:(NSString *)first andLast:(NSString *)last; @end </code></pre> <p>[person2.m]</p> <pre><code>#import "Person2.h" @implementation Person2 @synthesize first, last; - (id)init { self = [super init]; if (self) { } return self; } + (Person2 *)personWithFirst:(NSString *)first andLast:(NSString *)last { Person2 *person = [[Person2 alloc] init]; [person setFirst:first]; [person setLast:last]; return person; } - (NSString *)description { return [NSString stringWithFormat:@"%@ %@", [self first], [self last]]; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload