Cocoa NSArray/NSSet: -makeObjectsPerformSelector: vs. fast enumeration

后端 未结 5 1018
臣服心动
臣服心动 2021-02-13 09:50

I want to perform the same action over several objects stored in a NSSet.

My first attempt was using a fast enumeration:

for (id item in myS         


        
5条回答
  •  无人及你
    2021-02-13 10:25

    I would not use makeObjectsPerformSelector for the simple reason that it is the kind of call that you don't see all that often. Here is why for example - I need to add debugging code as the array is enumerated, and you really can't do that with makeObjectsPerformSelector unless you change how the code works in Release mode which is a real no no.

    for (id item in mySetOfObjects)
    {
        #if MY_DEBUG_BUILD
        if ([item isAllMessedUp])
            NSLog(@"we found that wily bug that has been haunting us"); 
        #endif
    
        [item action];
    }
    

    --Tom

提交回复
热议问题