Fever Review

description

Fever Review

Things Review

description

Things Review

Biologically Inspired Computation Series

description

Biologically Inspired Computation Series

Genetic Algorithms

description

Genetic Algorithms

Hopfield Network Simulation

description

Hopfield Network

Pattern Formation Simulation

description

AICA

Rising Sun Brushes

description

Rising Sun Brushes

Shit Boots

description

Shit Boots
Merry ChristmasiPhoto 09

Blocks: Coming to Objective-C Soon

Posted: December 26, 2008 at 9:55 pm | Cocoa, News Add Comments

From Mikeash.com:

I’m talking about a new addition to the language being created by Apple which adds anonymous functions to the language.

The uses and implications of this weren’t immediately apparent to me, but my interest was piqued as I continued reading the article.  Mike is quick to point out that anonymous functions would allow developers to innovate and essentially add features to the language.  Fast iteration was added to ObjC 2.0 in Leopard, allowing us to avoid NSEnumerator and go straight to a for( .. in .. ) statement. 

Blocks wouldn’t exactly have the same syntax, but you could easily implement this in your own code to get the feature.  There are several other examples, some notes that Blocks have access to local variables, and then the following point which is amazing:

Another place where blocks will make things much nicer is when dealing with callbacks. If you’ve ever written much Cocoa code you’ve probably had to write a sheet callback, and it’s a pain in the ass. If you need to pass variables through to the other side then it gets really frustrating with code like this:

 - (void)method {
        int foo;
        NSString *bar;

       /* do some work with those variables */
        NSDictionary *ctx = [[NSDictionary alloc]
            initWithObjectsAndKeys:
                [NSNumber numberWithInt:foo], @"foo",
                bar, @"bar",
                nil];
        [NSApp beginSheet:sheet
            modalForWindow:window
            modalDelegate:self
            didEndSelector:@selector(methodSheetDidEnd:
             returnCode:contextInfo:)
            contextInfo:ctx];
    }

    - (void)methodSheetDidEnd:(NSWindow *)sheet
            returnCode:(int)code
            contextInfo:(void *)ctx {
        NSDictionary *ctxDict = ctx;
        [ctxDict autorelease];

        int foo = [[ctxDict objectforKey:@"foo"] intValue];
        NSString *bar = [ctxDict objectForKey:@"bar"];
        /* do some more stuff with those variables
    }

 

Wow! What a pain that is. Since I removed all the stuff that does work, nearly everything that remains is just boilerplate. Horrible boilerplate whose only purpose is to tell the sheet who to call, and to pack up local information in a way that the sheet can give it back to you later on. Now let’s imagine we were redoing this API using blocks and see how it would look:

- (void)method {
        int foo;
        NSString *bar;
        /* do some work with those variables */
        [sheet beginSheetModalForWindow:window
            didEndBlock:^(int code){
            /* do stuff with foo */
            /* do stuff with bar */
            /* do stuff with code, or sheet, or window, etc */
        }];
    }

There are several other examples on the page and I think Cocoa developers will be very excited to see this addition to the language, presumably with Snow Leopard in 2009.

Read the article here.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview: