Your SlideShare is downloading. ×
  • Like
block introduce
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

block introduce

  • 89 views
Published

object-c block introduce

object-c block introduce

Published in Business , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
89
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Block特性 bangchen
  • 2. block在栈创建
  • 3. typedef int(^Blocky)(void);Blocky b[3];for (int i=0; i<3; i++) b[i] = ^{ return i;};for (int i=0; i<3; i++) printf("b %dn", b[i]());
  • 4. block copy• 我们用到的callback/GCD都是copy• 第一次copy,block放入堆里• 用到的值变量 制一份• 引用到的所有对象reatainCount+1
  • 5. __block
  • 6. 把变量放入堆,指向同一个地址 typedef void(^Blocky)(void); Blocky b; int i = 0; b = ^{ NSLog(@"i %d", i); }; i ++; b(); //0 typedef void(^Blocky)(void); Blocky b; __block int i = 0; b = ^{ NSLog(@"i %d", i); }; i ++; b(); //1
  • 7. 可修改 id x; __block id y; __block BOOL isRuned = NO; void (^block)(void) =^{ x = [NSString string]; // error y = [NSString string]; // works isRuned = YES; //works };
  • 8. 递归调用block typedef int(^Blocky)(int i); __block Blocky b = ^(int i){ if (i == 0 || i == 1) return i; return b(i-1) + b(i-2); }; NSLog(@"%d", b(10));
  • 9. 循环引用
  • 10. 问题 [self setCompletionBlock:^{ NSLog(@"%@", self.error); }]; self retainCount + 1self keep completeBlock - (void)setCompletionBlock:(ASIBasicBlock)aCompletionBlock { ! [completionBlock release]; ! completionBlock = [aCompletionBlock copy]; } self retain completionBlock, complationBlock retain self
  • 11. 解决(without ARC) __block id that = self; [self setCompletionBlock:^{ NSLog(@"%@", that.error); }]; or NSString *err = self.error; [self setCompletionBlock:^{ NSLog(@"%@", err); }];
  • 12. 解决(with ARC)iOS5: __weak id that = self; [self setCompletionBlock:^{ NSLog(@"%@", that.error); }];iOS4/5: __unsafe_unretained id that = self; [self setCompletionBlock:^{ NSLog(@"%@", that.error); }];
  • 13. 不易发现的循环引用ViewController* settingsViewController = [[[ViewController alloc] init] autorelease];settingsViewController.onUpdate = ^{ [self doUpdate]; } self.settingsViewController = settingsViewController;
  • 14. END