November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

22
November 7, 2009 Blocks - It’s What’s for Dinner Desert Code Camp 6

Transcript of November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Page 1: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

November 7, 2009

Blocks - It’s What’s for DinnerDesert Code Camp 6

Page 2: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

whoami

✤ Saul Mora (@casademora)

✤ Founding Panda, Magical Panda Software, LLC

✤ Custom iPhone development

Page 3: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Topics

✤ Closures

✤ The “Old Fashioned Way”

✤ Blocks - teh new hawtness

Page 4: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Closures

✤ Functional Languages

✤ In LISP since 1968

✤ Context + Function

Page 5: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

The “Old Fashioned Way”

✤ Global variables

✤ delegates

✤ Notifications

Page 6: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Properties

✤ Blocks are objects

✤ .... on the stack

✤ can copy from stack to heap

Page 7: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Blocks - Syntax

int (^worker)(int x, float y);

Return Type Block

Operator

Block Name

Parameters

Page 8: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

That...kinda...sucks

✤ typedef void (^awesome)(void);

✤ sorta better this way

Page 9: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

✤ x = ^{ printf("hello world\n"); }

Page 10: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

✤ x = ^(int a, char *b){ printf("a is %d and b is %s", a, b); }

Page 11: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

So, what’s the big deal?

✤ No need for extra objects sometimes

✤ Easier Multitasking development

Page 12: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

- (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 }

Old Skool

Page 13: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

- (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, or anything */ }]; }

Page 14: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

- (NSArray *)map:(id (^)(id))block { // takes an id, returns an id NSMutableArray *ret = [NSMutableArray array]; for(id obj in self) [ret addObject:block(obj)]; return ret; }

Page 15: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

- (NSArray *)select: (BOOL (^)(id obj))block { NSMutableArray *new = [NSMutableArray array]; for(id obj in self) if(block(obj)) [new addObject: obj]; return new; }

NSArray *longStrings = [strings select: ^ BOOL (id obj) { return [obj length] > 5; }];

Page 16: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

BasicBlock block; if(condition) block = ^{...}; else block = ^{...};

BasicBlock block; if(condition) block = [[^{...} copy] autorelease]; else block = [[^{...} copy] autorelease];

Page 17: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

More Reasons to Use ‘em

✤ Easier to multithread

✤ Grand Central Dispatch

Page 18: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Asynchronous Requests

@implementation NSURLConnection (BlocksAdditions) + (void)sendAsynchronousRequest: (NSURLRequest *)request completionBlock: (void (^)(NSData *data, NSURLResponse *response, NSError *error))block { NSThread *originalThread = [NSThread currentThread]; RunInBackground(^{ WithAutoreleasePool(^{ NSURLResponse *response = nil; NSError *error = nil; NSData *data = [self sendSynchronousRequest: request returningResponse: &response error: &error;]; RunOnThread(originalThread, NO, ^{ block(data, response, error); }); }); }); } @end

Page 19: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString: @"http://www.google.com/"]]; [NSURLConnection sendAsynchronousRequest: request completionBlock: ^(NSData *data, NSURLResponse *response, NSError *error){ NSLog(@"data: %ld bytes response: %@ error: %@", (long)[data length], response, error); }];

Page 20: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Is this available for iPhone?

✤ No

✤ Not Officially...

✤ Plausible Blocks for Leopard and iPhone

✤ ChooChoo for Leopard and iPhone

Page 21: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

More Reading

✤ http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/10

✤ http://en.wikipedia.org/wiki/Blocks_(C_language_extension)

✤ http://en.wikipedia.org/wiki/Closure_(computer_science)

✤ http://thirdcog.eu/pwcblocks/

✤ http://www.mikeash.com/?page=pyblog/friday-qa-2008-12-26.html

✤ http://www.mikeash.com/?page=pyblog/friday-qa-2009-08-14-practical-blocks.html

✤ http://www.mikeash.com/?page=pyblog/friday-qa-2008-12-26.html

Page 22: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6.

Wait’ll They Get A Load a’ Me!

✤ HTML5 Introduction/Overview - Next!

✤ Developer Ignite - November 11, 2009

✤ Gilbert Community Center

✤ Refactoring Databases

✤ Follow @intelevents