Advanced Enumerations for Cocoa Collection Classes

Tags: enumeration, NSArray, NSDictionary, NSSet

intui — 2007/12/01

This framework adds the standard SmallTalk enumeration patterns to the main cocoa collection classes, using functions pointers.

The benefit is instead of having to write something like this:

NSEnumerator *enumerator = [testArray objectEnumerator];
id object;
id tempArray = [NSMutableArray arrayWithCapacity:[self count]]; 
while (object = [enumerator nextObject]) {
    if([object intValue] < 0)
         [tempArray addObject:object];
}

You can write this:

BOOL selectNegative(id each, void* context){
        return ([each intValue] < 0);  
}
id tempArray2 = [testArray selectUsingFunction:selectNegative context:nil];


Related Traces

Add a new comment or fragment

Fields marked with * are mandatory.


User Name

Fragment (zip files only)

Mac OS X Version

License

Screenshot

Comment * Help

Password (if you plan to edit this entry later)