Archive for December 8, 2003

Monday, December 8, 2003 [Tweets] [Favorites]

Cocoa Enumeration

Jonathan Rentzsch is back with a post about the macros he uses to tame Cocoa’s enumeration idiom. Here’s the macro I’ve been using:

#define foreach(object, enumerator) 
    id mjtForeachEnumerator ## object = (enumerator); 
    if ( [mjtForeachEnumerator ## object respondsToSelector:@selector(objectEnumerator)] ) 
        mjtForeachEnumerator ## object = [mjtForeachEnumerator ## object objectEnumerator]; 
    SEL mjtNextObjectSEL ## object = @selector(nextObject); 
    IMP mjtNextObjectIMP ## object = [mjtForeachEnumerator ## object methodForSelector:mjtNextObjectSEL ## object]; 
    id object; 
    while ( (mjtForeachEnumerator ## object) && 
            object = mjtNextObjectIMP ## object(mjtForeachEnumerator ## object, mjtNextObjectSEL ## object) )