iphone - 使用块从数组中获取子数组非常令人困惑

发布于 2024-10-21 03:27:05 字数 732 浏览 1 评论 0原文

我有一个对象数组(json 对象) 每个对象都具有以下性质:

{
        author = "<null>";
        category =         {
            "created_at" = "2011-02-06T18:11:39Z";
            id = 4;
            name = lawyers;
        };
        "created_at" = "<null>";
        id = 693;
        "mobile_user_id" = "<null>";
        "rating_count" = 0;
        status = 1;
        text = "A brain walks into a bar and says, \"I'll have a pint of beer please. \"The barman looks at him and says \"Sorry, I can't serve you.\" \"Why not?\" asks the brain. \"You're already out of your head.\"";
        title = "A brain goes to a local bar";
    }

从这些对象数组中,我想找到类别 id = 4 的对象并创建一个子数组。

有人可以帮助我使用块并从数组中获取子数组吗?

I'm having an array of objects (json objects)
each object is of following nature:

{
        author = "<null>";
        category =         {
            "created_at" = "2011-02-06T18:11:39Z";
            id = 4;
            name = lawyers;
        };
        "created_at" = "<null>";
        id = 693;
        "mobile_user_id" = "<null>";
        "rating_count" = 0;
        status = 1;
        text = "A brain walks into a bar and says, \"I'll have a pint of beer please. \"The barman looks at him and says \"Sorry, I can't serve you.\" \"Why not?\" asks the brain. \"You're already out of your head.\"";
        title = "A brain goes to a local bar";
    }

From those array of objects, I want to find objects whose category has id = 4 and make a sub array.

Can some one help me in using blocks and get sub array from array please?

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

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

发布评论

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

评论(1

像你 2024-10-28 03:27:05

我想通过“使用块”,你的意思是使用 NSArray 的方法,例如

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

然后它看起来像

NSIndexSet *indexes = [yourArray indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return (BOOL)([[obj valueForKeyPath:@"category.id"] intValue] == 4);
}];
NSArray *filteredArray = [yourArray objectsAtIndexes:indexes];

这就是你所要求的,但我宁愿使用 filteredArrayUsingPredicate: 像这样:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"category.id = %@", [NSNumber numberWithInt:4]];
NSArray *filteredArray = [yourArray filteredArrayUsingPredicate:pred];

I guess by "using blocks", you mean using a method of NSArray like

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

Then it would look like

NSIndexSet *indexes = [yourArray indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return (BOOL)([[obj valueForKeyPath:@"category.id"] intValue] == 4);
}];
NSArray *filteredArray = [yourArray objectsAtIndexes:indexes];

That's for what you asked, but I'd rather use filteredArrayUsingPredicate: like this:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"category.id = %@", [NSNumber numberWithInt:4]];
NSArray *filteredArray = [yourArray filteredArrayUsingPredicate:pred];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文