Declaring and Using Blocks

One method of using blocks is to create a block variable, which can be assigned a block and then executed inline in code. A block variable declaration can have a name, can declare parameter types, and can optionally declare a return type (the compiler can infer the return type, but it is good coding form to declare it).

NSString* (^repeatString) (int, NSString*);

In this case, NSString* at the left of the line of code declares the return type. In parentheses, after the caret symbol (^), repeatString is the name of the declared block. The second set of parentheses declares that two parameters are required for the block, first an int and second an NSString*. After these are declared, the ...

Get iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.