Objective-C helpers

Posted by Daniel Vela on December 4, 2011

Blocks

A block of code is an structure with this format:

^ <return_type> <parameters> {<code>}   

like:

^BOOL(id str){return [str length] > num;}  
^{[local doSomething];}  

This structures can be assigned or passed as a parameter or stacked.

Iterations

NSArray* array;  
for(id e in array) {...e...}  
NSDictionary* dictionary;  
for(id k in dictionary){...k...}  
[array enumerateObjectsWithOptions:NSEnumerationReverse   
                        usingBlock:^(id e,NSUInteger idx,BOOL* stop){...e...idx}];  
[dictionary enumerateKeysAndObjectsUsingBlock:  
         ^(id k,id obj,BOOL* stop){...k...obj;}]  

Memory management

When you have two objects that have a reference to each other, use this convention to avoid release locks: Downlinks member instance use retain. Uplinks member instance don’t use retain.

This avoid memory locks like this one:

obejcts never released