数组传递测试的对象数组 [英] Array of objects from array passing test

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

问题描述

我有一个NSArray对象,它有一个属性 id

I have an NSArray of objects, which have a property id.

然后我有另一个NSArray选择的ID。

I then have another NSArray with a selection of ids.

我需要获取第一个数组中第二个数组中列出id的所有对象。

I need to get all the objects in the first array which have the ids listed in the second array.

是否可以在没有for循环的情况下执行此操作(1代for循环没问题,但我想避免它)。我知道如何用2 for循环来做这个,但这看起来非常低效。所以基本上我正在寻找最有效的方法。

Is it possible to do this without for loops (well 1 for loop is ok, but I'd like to avoid it). I know how to do this with 2 for loops, but this seems very inefficient. So basically I'm looking for the most efficient way.

(这是一个NSURL顺便说一句,所以它不能是任何整数特定的)

(The Id is an NSURL btw, so it can't be anything integer specific)

推荐答案

没有循环!

NSArray *arrayOfIdentifiers = ...;
NSArray *arrayOfObjects = ...;
NSPredicate *filter = [NSPredicate predicateWithFormat:@"id IN %@", arrayOfIdentifier];
NSArray *filteredObjects = [arrayOfObjects filteredArrayUsingPredicate:filter];

好吧,没有你写的循环。 filteredArrayUsingPredicate里面可能有循环:

Well, no loops that you write. There are probably loops inside filteredArrayUsingPredicate:.

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

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