循环通过NSMutableArray自定义对象的内存最有效的方法是什么? 我需要检查数组中每个对象的值,并返回该数组中对象的数量。
答案 0 :(得分:35)
for (WhateverYourClassNameIs *whateverNameYouWant in yourArrayName) {
[whateverNameYouWant performSelector];
more code here;
}
它被称为快速枚举,是Objective C 2.0的新功能,可在iPhone上使用。
答案 1 :(得分:17)
我可能只是使用一个谓词,就像这样:
NSArray * filtered = [myArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"aProperty = %@", @"someValue"]];
NSLog(@"number of items where aProperty = someValue: %d", [filtered count]);
编辑:此代码在功能上等同于:
NSMutableArray * filtered = [NSMutableArray array];
for (MyCustomObject * object in myArray) {
if ([[object aProperty] isEqual:@"someValue"]) {
[filtered addObject:object];
}
}