过滤器在ios中不起作用

时间:2015-07-14 12:04:18

标签: ios filtering nspredicate

我使用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以获得预期的输出

2 个答案:

答案 0 :(得分:1)

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

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

在上面的代码中

如果您提供 LIKE ,则会提供相同的价格数据。

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

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

答案 1 :(得分:0)

你有这样的数组

NSArray *arr = @[@{
                     @"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"
                     }];

你必须在这个数组上使用谓词,如

   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Price like %@",@"100"];
NSArray *arr2 = [arr filteredArrayUsingPredicate:predicate];

并且您从数组获得价格为100的所有值