【问题标题】:NSPredicate not working with array of dictionariesNSPredicate 不适用于字典数组
【发布时间】:2011-09-05 07:32:29
【问题描述】:

我有一个格式如下的 NSPredication:@"Status CONTAINS[cd] shipped"

我有一个字典数组,这是一个例子:

{
        {
        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;
    }
)

但它返回一个空数组。我究竟做错了什么?谢谢

【问题讨论】:

    标签: objective-c cocoa-touch nsarray nsdictionary nspredicate


    【解决方案1】:

    你想要:

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

    或者:

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

    你的问题是你的谓词格式有shipped,而它应该有'shipped'。缺少单引号意味着它试图将[dictionary valueForKey:@"Status"][dictionary valueForKey:@"shipped"] 进行比较,而不是与字符串文字@"shipped" 进行比较。

    【讨论】:

      【解决方案2】:

      您可以对字符串使用关键字MATCHESCONTAINS 等。由于 Status 不是字符串,我猜你需要像这样检查它,

      [NSPredicate predicateWithFormat:@"Status = %d", Shipped];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多