Tuesday, May 28, 2013

Selector on array - better way for fast enumeration

This entry will be short. It is continuation of last post about selectors. 
One of my students gave me a question how makeObjectsPerformSelector: works.

I will try to explain it fast. makeObjectsPerformSelector: is part od NSArray inmplementation. There is group of methods stacked under label 
"SendingMessagesToElements".

So how it works? We know that the array is collection of elements. Calling makeObjectsPerformSelector: on array will cause calling method on each element of array. It the object will respond to selector (if it will have implemented method) the method will run. 

There is also option to call selector with argument to each object in array makeObjectsPerformSelector:withObject:
So as I wrote in pervious blog entry about selectors - you can group arguments into one and pass them as collection by selector. 

So answer is: makeObjectsPerformSelector:  is a nice alternative for fast enumeration.

No comments:

Post a Comment