NSPredicate not working with array of dictionaries

2019-06-24 00:57发布

I have an NSPredication with format like this:@"Status CONTAINS[cd] shipped"

I have an array of dictionary, here's an example:

{
        {
        Category = "Category 4";
        Comp = "Category 4";
        Depth = 02;
        Grade = New;
        Length = 02;
        Location = "Thousand Oaks, CA";
        Name = "3ply Mat";
        Owner = "Owner 3";
        PUP = 2;
        Status = Shipped;
        "Unit Number" = 20110507113852351;
        Width = 02;
    },
        {
        Category = "Category 4";
        Comp = "Category 4";
        Depth = 02;
        Grade = New;
        Length = 02;
        Location = "Thousand Oaks, CA";
        Name = "3ply Mat";
        Owner = "Owner 3";
        PUP = 2;
        Status = Shipped;
        "Unit Number" = 20110516094641587;
        Width = 02;
    }
)

But it's returning an empty array. What am I doing wrong? Thanks

2条回答
何必那么认真
2楼-- · 2019-06-24 01:26

You want:

[NSPredicate predicateWithFormat:@"Status =[cd] 'shipped'"];

Or:

[NSPredicate predicateWithFormat:@"Status =[cd] %@", @"shipped"];

Your problem was that your predicate format had shipped, when it should've had 'shipped'. The lack of singles quotes meant it was trying to compare [dictionary valueForKey:@"Status"] to [dictionary valueForKey:@"shipped"], instead of to the string literal @"shipped".

查看更多
Fickle 薄情
3楼-- · 2019-06-24 01:29

You can use the keywords MATCHES, CONTAINS etc., against strings. As Status is not a string, I guess, you need to check it like this,

[NSPredicate predicateWithFormat:@"Status = %d", Shipped];
查看更多
登录 后发表回答