november 7, 2009 blocks - its whats for dinner desert code camp 6
TRANSCRIPT
![Page 1: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/3.jpg)
Topics
✤ Closures
✤ The “Old Fashioned Way”
✤ Blocks - teh new hawtness
![Page 4: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/4.jpg)
Closures
✤ Functional Languages
✤ In LISP since 1968
✤ Context + Function
![Page 5: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/5.jpg)
The “Old Fashioned Way”
✤ Global variables
✤ delegates
✤ Notifications
![Page 6: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/9.jpg)
✤ x = ^{ printf("hello world\n"); }
![Page 10: November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/10.jpg)
✤ 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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/12.jpg)
- (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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/13.jpg)
- (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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/14.jpg)
- (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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/15.jpg)
- (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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061305/551420e0550346ec488b5811/html5/thumbnails/22.jpg)
Wait’ll They Get A Load a’ Me!
✤ HTML5 Introduction/Overview - Next!
✤ Developer Ignite - November 11, 2009
✤ Gilbert Community Center
✤ Refactoring Databases
✤ Follow @intelevents