Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 通过测试的数组中的对象数组_Iphone_Objective C_Ipad_Nsarray_Nspredicate - Fatal编程技术网

Iphone 通过测试的数组中的对象数组

Iphone 通过测试的数组中的对象数组,iphone,objective-c,ipad,nsarray,nspredicate,Iphone,Objective C,Ipad,Nsarray,Nspredicate,我有一组对象,它们有一个属性id 然后我有另一个NSArray和一系列ID 我需要获取第一个数组中的所有对象,这些对象的ID在第二个数组中列出 是否可以在没有for循环的情况下执行此操作(1 for循环是可以的,但我希望避免它)。我知道如何使用2个for循环来实现这一点,但这似乎效率很低。所以基本上我在寻找最有效的方法 (Id是一个NSURL btw,因此它不能是任何整数特定的)您需要一个交叉点os集合 NSMutableSet *set1=[[[NSMutableSet alloc] init

我有一组对象,它们有一个属性
id

然后我有另一个NSArray和一系列ID

我需要获取第一个数组中的所有对象,这些对象的ID在第二个数组中列出

是否可以在没有for循环的情况下执行此操作(1 for循环是可以的,但我希望避免它)。我知道如何使用2个for循环来实现这一点,但这似乎效率很低。所以基本上我在寻找最有效的方法


(Id是一个NSURL btw,因此它不能是任何整数特定的)

您需要一个交叉点os集合

NSMutableSet *set1=[[[NSMutableSet alloc] initWithArray:array1] autorelease];
NSMutableSet *set2=[[NSMutableSet alloc] initWithArray:array2];
[set1 intersectSet:set2];
[set2 release];
NSArray *newArray=[set1 allObjects];
没有循环

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

嗯,没有你写的循环。
filteredarrayingpredicate:

很抱歉忘记添加循环,我需要保持顺序。即使使用大数组,循环也不会花费太多时间。我想比较物体需要时间。。。你能说得更具体一点吗?我想我写的循环会比现有方法中的循环效率低一些?谢谢:)@Jonathan。不一定。。。无论是你写的还是苹果写的,
for
循环都具有相同的效率。这里真正的问题是,如果苹果已经编写了满足您需要的代码,那么您就没有必要重新编写。这一点很好。顺便说一句,第三行末尾有一个双引号。