Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

makeObjectsPerformSelector:

I want to make all objects in an array perform a selector. I've discovered the appropriately named makeObjectsPerformSelector: method, but I have a question with it. If I use it on an array, will it change the existing array or return a new one? If it modifies the existing object, what be the easiest way to return a new array with the selector applied?

like image 413
Allyn Avatar asked Feb 18 '09 22:02

Allyn


1 Answers

makeObjectsPerformSelector: is going to run that selector against every object in the array. If those objects are modified by the selector they will be modified. It does not return anything. Now, there is a catch, which that by default most copies in Cocoa are shallow copies, which means you get a new array, but the underlying objects it points to are the same objects. You will need to use initWithArray:copyItems to make it copy the root level items as well. If you want a new array containing the altered objects as well as the old array do something like this:

NSArray *newArray = [[NSArray alloc] initWithArray:oldArray copyItems:YES];
[newArray makeObjectsPerformSelector:@selector(doSomethingToObject)];
like image 149
Louis Gerbarg Avatar answered Sep 24 '22 06:09

Louis Gerbarg