views:

121

answers:

2

Does Objective-C support blocks "a la Smalltalk"?

In Smalltalk, blocks are similar to "closures" or "lambda-expressions" or "nameless functions" found in other languages.

+4  A: 

Yep, take this example:

[[myString componentsSeparatedByString:@"\n"] enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSString *line = (NSString *)obj;
    //do what your going to do with line...
}];
Richard J. Ross III
+9  A: 

Yes: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html

Out of the box, they're only supported in the version of Objective-C 2.0 that comes with XCode 3.2. This means you'll need Snow Leopard if you want to use the official build tools. A potential work-around for 10.5 is described here: http://thirdcog.eu/pwcblocks/#leoiphone

Paul Baumgart
But there are distinct in one aspect: the way you return from it.
mathk