Is there a more concise way to do the following in iOS?
Suppose we have:
NSArray *existingArray;
and also our object self has a method someMethod which can take the objects in existingArray as input and returns objects of class Foo. We could create a new array *derivedArray by applying someMethod to the objects in existingArray as follows:
NSMutableArray *derivedArray = [[NSMutableArray alloc]init];
for (id obj in existingArray) {
[derivedArray addObject: [self someMethod: obj]];
}
My question is, is there a more concise way to do that? Something like:
NSArray *derivedArray = [existingArray arrayByMappingMethod: someMethod withObject: self];
No. There is a
makeObjectsPerformSelector:method (and a few other related ones), but none allow you to get a result from the calls. You could, of course, add your method as a category on NSArray, but Apple has not provided a method to do this.In terms of performance improvements, change
to
This will allow the array to allocate memory for all of the objects at once, instead of reallocating every time you add an object.