Download presentation
Presentation is loading. Please wait.
Published byAvery Bain Modified over 11 years ago
1
November 7, 2009 Blocks - Its Whats for Dinner Desert Code Camp 6
2
whoami Saul Mora (@casademora) Founding Panda, Magical Panda Software, LLC Custom iPhone development
3
Topics Closures The Old Fashioned Way Blocks - teh new hawtness
4
Closures Functional Languages In LISP since 1968 Context + Function
5
The Old Fashioned Way Global variables delegates Notifications
6
Properties Blocks are objects.... on the stack can copy from stack to heap
7
Blocks - Syntax int (^worker)(int x, float y); Return Type Block Operator Block Name Parameters
8
That...kinda...sucks typedef void (^awesome)(void); sorta better this way
9
x = ^{ printf("hello world\n"); }
10
x = ^(int a, char *b){ printf("a is %d and b is %s", a, b); }
11
So, whats the big deal? No need for extra objects sometimes Easier Multitasking development
12
- (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
13
- (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 */ }]; }
14
- (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; }
15
- (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; }];
16
BasicBlock block; if(condition) block = ^{...}; else block = ^{...}; BasicBlock block; if(condition) block = [[^{...} copy] autorelease]; else block = [[^{...} copy] autorelease];
17
More Reasons to Use em Easier to multithread Grand Central Dispatch
18
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
19
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); }];
20
Is this available for iPhone? No Not Officially... Plausible Blocks for Leopard and iPhone Plausible Blocks ChooChoo for Leopard and iPhone ChooChoo
21
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/Blocks_(C_language_extension http://en.wikipedia.org/wiki/Closure_(computer_science) 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
22
Waitll They Get A Load a Me! HTML5 Introduction/Overview - Next! Developer Ignite - November 11, 2009 Gilbert Community Center Refactoring Databases Follow @intelevents
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.