[英]Filtering NSMutableArray of NSDictionary with NSPredicate
I'm having an NSObject entity for storing some data like name, id from server 我有一个NSObject实体,用于存储一些数据,例如来自服务器的名称,ID
NSObject file looks like below, 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;
}
I'm storing the values from server to an NSMutableArray by, 我将值从服务器存储到NSMutableArray,
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:responseData[@"result"]];
for (NSDictionary *dict in array) {
MyEntity *ent = [[MyEntity alloc] initWithJSON:dict];
[myArray addObject:ent];
}
Now, I want to filter that NSMutableArray by some usernames using NSPredicate. 现在,我想使用NSPredicate通过某些用户名过滤该NSMutableArray。 I've tried below code, 我试过下面的代码,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY MyEntity.userName == %@", @"popo"];
NSArray *predicateFilteredArray = [myArray filteredArrayUsingPredicate:predicate];
[myArray removeAllObjects];
[myArray addObjectsFromArray:predicateFilteredArray];
it's not working. 它不起作用。 How can I achieve this? 我该如何实现?
You just do not need to check predicate with its entity name write simple predicate and it will work I mean replace your code 您只需要检查谓词及其实体名称即可编写简单谓词,它将起作用,我的意思是替换您的代码
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY MyEntity.userName == %@", @"popo"];
With 用
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName == %@", @"popo"];
and it will work. 它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.