NSPredicate 不适用于字典数组

发布于 2024-11-10 14:38:21 字数 839 浏览 0 评论 0原文

我有一个 NSPredicate ,其格式如下:@"Status CONTAINS[cd] returned"

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

塔塔猫 2024-11-17 14:38:21

您想要:

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

或者:

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

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

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".

梦里人 2024-11-17 14:38:21

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

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文