我正在尝试过滤我保留在我的应用程序中的员工联系人列表的结果,但出现以下错误:'Can't use in/contains operator with collection LAST (not a collection)'
我用 NSPredicate 命令尝试了几种变体,self、self.last、employee.last、last == 'smith'(这个不会产生错误但不会返回任何结果)。
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);
[person2.h]
@interface Person2 : NSObject
{
@private
}
@property (nonatomic, retain) NSString *first;
@property (nonatomic, retain) NSString *last;
+ (Person2 *)personWithFirst:(NSString *)first andLast:(NSString *)last;
@end
[人2.m]
#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