带有子数组的 NSPredicate [英] NSPredicate with sub array

查看:54
本文介绍了带有子数组的 NSPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究以下类型的数据结构:

<前>{author = "作者 1";频道 ID = (7);},{author = "作者 2";频道 ID = (7、1);},,{author = "作者 3";频道 ID = (3、7);}

我想构建一个数组,其中包含 channeldIds = 7 的所有项目.

我正在按照以下谓词进行尝试:

NSPredicate * filterById = [NSPredicate predicateWithFormat:@"channelIds[0] = '7'"];

  • 我可以在这里做我想做的事吗?
  • 我可以有 x 个 channelId,有什么方法可以在谓词中迭代吗?

蒂亚,S.

解决方案

更简单:

NSPredicate *filterById = [NSPredicate predicateWithFormat:@"channelIds CONTAINS %@", @"7"];

这里假设您的 channelIds 值是一个 strings 数组.如果它是一个数字数组 (NSNumber),那么您的格式字符串将只是 @"channelIds CONTAINS 7".

I am looking at the following type of data structure:

{
    author = "Author 1";
    channelIds =         (
        7
    );
},
{
    author = "Author 2";
    channelIds =         (
        7,
        1
    );
},
,
{
    author = "Author 3";
    channelIds =         (
        3,
        7
    );
}

I'd like to build an array consisting of all items where channeldIds = 7.

I was trying things along the lines of the following predicate:

NSPredicate * filterById = [NSPredicate predicateWithFormat:@"channelIds[0] = '7'"];

  • How can I do what I'd like here?
  • I can have an x number of channelIds, is there any way to iterate in the predicate?

Tia, S.

解决方案

Easier:

NSPredicate *filterById = [NSPredicate predicateWithFormat:@"channelIds CONTAINS %@", @"7"];

This is assuming that your channelIds value is an array of strings. If it's an array of numbers (NSNumber), then your format string would simply be @"channelIds CONTAINS 7".

这篇关于带有子数组的 NSPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆