november 7, 2009 blocks - its whats for dinner desert code camp 6
Post on 26-Mar-2015
221 Views
Preview:
TRANSCRIPT
November 7, 2009
Blocks - It’s What’s for DinnerDesert Code Camp 6
whoami
✤ Saul Mora (@casademora)
✤ Founding Panda, Magical Panda Software, LLC
✤ Custom iPhone development
Topics
✤ Closures
✤ The “Old Fashioned Way”
✤ Blocks - teh new hawtness
Closures
✤ Functional Languages
✤ In LISP since 1968
✤ Context + Function
The “Old Fashioned Way”
✤ Global variables
✤ delegates
✤ Notifications
Properties
✤ Blocks are objects
✤ .... on the stack
✤ can copy from stack to heap
Blocks - Syntax
int (^worker)(int x, float y);
Return Type Block
Operator
Block Name
Parameters
That...kinda...sucks
✤ typedef void (^awesome)(void);
✤ sorta better this way
✤ x = ^{ printf("hello world\n"); }
✤ x = ^(int a, char *b){ printf("a is %d and b is %s", a, b); }
So, what’s the big deal?
✤ No need for extra objects sometimes
✤ Easier Multitasking development
- (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
- (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 */ }]; }
- (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; }
- (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; }];
BasicBlock block; if(condition) block = ^{...}; else block = ^{...};
BasicBlock block; if(condition) block = [[^{...} copy] autorelease]; else block = [[^{...} copy] autorelease];
More Reasons to Use ‘em
✤ Easier to multithread
✤ Grand Central Dispatch
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
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); }];
Is this available for iPhone?
✤ No
✤ Not Officially...
✤ Plausible Blocks for Leopard and iPhone
✤ ChooChoo for Leopard and iPhone
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
Wait’ll They Get A Load a’ Me!
✤ HTML5 Introduction/Overview - Next!
✤ Developer Ignite - November 11, 2009
✤ Gilbert Community Center
✤ Refactoring Databases
✤ Follow @intelevents
top related