gpt4 book ai didi

ios - 根据字典键过滤 NSDictionary 的 NSArray

转载 作者:行者123 更新时间:2023-11-29 03:23:25 26 4
gpt4 key购买 nike

我有一个 NSArray 的 NSDictionary,我想从中过滤掉一个特定的字典。使用下面的代码,我得到了 nil matchedDicts。我做错了什么吗?

NSPredicate *p = [NSPredicate predicateWithFormat:@"locationID = %@", @"949"];
NSArray *matchedDicts = [aLocationsList filteredArrayUsingPredicate:p];

{
locationID = 2023;
locationName = "Customer";
sortOrder = 0;
},
{
locationID = 2024;
locationName = "My Loc";
sortOrder = 1;
},
{
locationID = 949;
locationName = "His Loc";
sortOrder = 2;
}
)

最佳答案

您可以使用 block 格式来代替:

-(NSArray*)filterArrayOfDictionaries:(NSArray*)arr
{
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *dict, NSDictionary *bindings) {
NSNumber *locationID = dict[@"locationID"];

if([locationID intValue]==949)
{
return YES;
}
return NO;
}];

return [arr filteredArrayUsingPredicate:predicate];
}

关于ios - 根据字典键过滤 NSDictionary 的 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20791605/

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