gpt4 book ai didi

ios - 过滤器在 ios 中不起作用

转载 作者:行者123 更新时间:2023-11-29 01:51:04 25 4
gpt4 key购买 nike

我使用 NsPredicate 按一个和多个值过滤 NSMutableArray,首先我尝试按价格过滤,我已将 NSDictionary 值保存到 NSMutableArray (即)resultArray,这是我的代码帮助我,

    for (int i=0; i<=resultArray.count; i++)
{
NSDictionary *dict=resultArray[i];
NSLog(@"Dict %@",dict);
NSPredicate *predit=[NSPredicate predicateWithFormat:@"(Price == %@)", @"100"];
NSArray *resultAr = [[dict allValues] filteredArrayUsingPredicate:predit];
NSLog(@"Output %@",resultAr);
}


Dict {
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
}

结果数组是:

 Result (
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
},
{
Name = "Black Gram";
Percentage = 0;
Price = 56;
},
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Double Beans";
Percentage = 0;
Price = 95;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Green Moong Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Ground Nut";
Percentage = 0;
Price = 140;
},
{
Name = "Moong Dal";
Percentage = 0;
Price = 75;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
},
{
Name = "Toor Dal";
Percentage = 0;
Price = 150;
}
)

预期输出为

(
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
}
)

但它给出了错误

 reason: '[<__NSCFNumber 0x146db5e0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Price. 

上面的代码对于过滤来说是正确的,否则给我一个过滤 resultArray 以获得预期输出的想法

最佳答案

按照下面的代码。我在这里使用谓词。

        NSPredicate *predit=[NSPredicate predicateWithFormat:@"Price like %@",100];
NSArray *resultAr = [resultArray filteredArrayUsingPredicate:predit];

在上面的代码中

如果您给LIKE,它会给出相同的价格数据。

您必须从代码中删除或删除 [dict allValues]。

您需要添加 resultArray 而不是 [dict allValues]。因为它是数组。

关于ios - 过滤器在 ios 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31406074/

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