我有一个NSObject实体用于存储一些数据,如来自服务器的名称,id
NSObject文件如下所示,
@interface MyEntity : NSObject
@property(nonatomic,strong)NSString *userId;
@property(nonatomic,strong)NSString *userName;
- (id)initWithJSON:(NSDictionary *)dataDict;
@end
- (id)initWithJSON:(NSDictionary *)dataDict
{
if (self = [super init])
{
self.userId = [dataDict objectForKeyNotNull:@"userId"] ;
self.userName = [dataDict objectForKeyNotNull:@"userName"] ;
}
return self;
}
我将值从服务器存储到NSMutableArray,
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:responseData[@"result"]];
for (NSDictionary *dict in array) {
MyEntity *ent = [[MyEntity alloc] initWithJSON:dict];
[myArray addObject:ent];
}
现在,我想使用NSPredicate通过某些用户名过滤NSMutableArray。我试过下面的代码,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY MyEntity.userName == %@", @"popo"];
NSArray *predicateFilteredArray = [myArray filteredArrayUsingPredicate:predicate];
[myArray removeAllObjects];
[myArray addObjectsFromArray:predicateFilteredArray];
它不起作用。我怎样才能做到这一点?
答案 0 :(得分:4)
你只需要检查谓词的实体名称是否写为简单谓词就可以了,我的意思就是替换你的代码
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY MyEntity.userName == %@", @"popo"];
使用
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName == %@", @"popo"];
它会起作用。