基于键对 NSMutableArray 进行排序,谓词不起作用?
Sorting NSMutableArray based on Key with Predicates not working?
我有一个包含超过 150 个对象的 NSMutableArray,我需要根据此键 "name" 与输入名称匹配来创建一个新数组。
所以我从 Whosebug 得到了这个谓词代码
NSMutableArray *listForm=[[NSMutableArray alloc]init]; //copy Original Array
listForm=[arr valueForKey:@"data"]; //copied
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", [selectedDrug valueForKey:@"name"]]; //checking name
NSLog(@"%@",[selectedDrug valueForKey:@"name"]); //my input name for matching
[listForm filteredArrayUsingPredicate:predicate];
NSLog(@"%@",listForm);//final result
现在得到了正确的结果,但同时得到了额外的对象
比如喜欢
考虑这是我的大对象列表
[{
name:"john",
location:"washington",
age:23
},{
name:"Zirad kan",
location:"iceland",
age:23
},{
name:"john",
location:"usa",
age:43
},{
name:"riya",
location:"india",
age:20
},{
name:"Rachel",
location:"netherland",
age:33
},{
name:"john Mark",
location:"washington",
age:23
},{
name:"john Doe",
location:"washington",
age:23
}]
从这里我需要所有名称完全匹配 "john"
所以结果会是这样
[{
name:"john",
location:"washington",
age:23
},{
name:"john",
location:"usa",
age:43
}]
但是当我使用上面的代码时,我得到的是最终结果
[{
name:"john",
location:"washington",
age:23
},{
name:"john",
location:"usa",
age:43
},{
name:"john Mark",
location:"washington",
age:23
},{
name:"john Doe",
location:"washington",
age:23
}]
如何删除 "John Doe" 和 "John Mark " 就像我只需要匹配 "john".
的特定文本一样
请帮帮我
[NSArray filteredArrayUsingPredicate:]
returns 过滤后的数组,所以:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@",
selectedDrug[@"name"]];
NSArray *filtered = [arr[@"data"] filteredArrayUsingPredicate:predicate];
注意:这假设 arr
和 selectedDrug
都是 NSDictionary
个实例。
我有一个包含超过 150 个对象的 NSMutableArray,我需要根据此键 "name" 与输入名称匹配来创建一个新数组。
所以我从 Whosebug 得到了这个谓词代码
NSMutableArray *listForm=[[NSMutableArray alloc]init]; //copy Original Array
listForm=[arr valueForKey:@"data"]; //copied
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", [selectedDrug valueForKey:@"name"]]; //checking name
NSLog(@"%@",[selectedDrug valueForKey:@"name"]); //my input name for matching
[listForm filteredArrayUsingPredicate:predicate];
NSLog(@"%@",listForm);//final result
现在得到了正确的结果,但同时得到了额外的对象
比如喜欢 考虑这是我的大对象列表
[{
name:"john",
location:"washington",
age:23
},{
name:"Zirad kan",
location:"iceland",
age:23
},{
name:"john",
location:"usa",
age:43
},{
name:"riya",
location:"india",
age:20
},{
name:"Rachel",
location:"netherland",
age:33
},{
name:"john Mark",
location:"washington",
age:23
},{
name:"john Doe",
location:"washington",
age:23
}]
从这里我需要所有名称完全匹配 "john"
所以结果会是这样
[{
name:"john",
location:"washington",
age:23
},{
name:"john",
location:"usa",
age:43
}]
但是当我使用上面的代码时,我得到的是最终结果
[{
name:"john",
location:"washington",
age:23
},{
name:"john",
location:"usa",
age:43
},{
name:"john Mark",
location:"washington",
age:23
},{
name:"john Doe",
location:"washington",
age:23
}]
如何删除 "John Doe" 和 "John Mark " 就像我只需要匹配 "john".
的特定文本一样请帮帮我
[NSArray filteredArrayUsingPredicate:]
returns 过滤后的数组,所以:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@",
selectedDrug[@"name"]];
NSArray *filtered = [arr[@"data"] filteredArrayUsingPredicate:predicate];
注意:这假设 arr
和 selectedDrug
都是 NSDictionary
个实例。