gpt4 book ai didi

ios - 基于键对 NSMutableArray 进行排序,谓词不起作用?

转载 作者:行者123 更新时间:2023-11-29 10:20:49 25 4
gpt4 key购买 nike

我有一个包含 150 多个对象的 NSMutableArray,我需要根据与输入名称匹配的键“名称”创建一个新数组。

所以我从 stackoverflow 得到了这个谓词代码

    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:]返回过滤后的数组,因此:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@",
selectedDrug[@"name"]];
NSArray *filtered = [arr[@"data"] filteredArrayUsingPredicate:predicate];

注意:这假设 arrselectedDrug 都是 NSDictionary 实例。

关于ios - 基于键对 NSMutableArray 进行排序,谓词不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35364718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com