1、输出的值是多少?为神么?
- (void)demo1 {
NSInteger x = 10;
NSLog(@"前%p",&x);
void(^demoBlock)() = ^ {
NSLog(@"%ld",x);
NSLog(@"中%p",&x);
};
x = 20;
NSLog(@"后%p",&x);
demoBlock();
}
__block的使用注意为题
- (void)demo2 {
__block NSInteger x = 10;
NSLog(@"%ld",x);
NSLog(@"前%p",&x);
void (^ myBlock)() = ^ {
x = 90;
NSLog(@"%ld",x);
NSLog(@"中%p",&x);
};
x = 20;
myBlock();
NSLog(@"%ld",x);
NSLog(@"后%p",&x);
}
3,block中改变MutableString的值
void demo3(){
NSMutableString *str = [NSMutableString stringWithFormat:@"zhangsan"];
NSLog(@"定义前 %p %p",str,&str);
void (^myBlock)() = ^ {
[str setString:@"lisi"];
NSLog(@"in block %p %p",str,&str);
NSLog(@"%@",str);
};
myBlock();
[str setString:@"wangwu"];
NSLog(@"定义后 %p %p",str,&str);
NSLog(@"%@",str);
<! 12:45:18.832 blockTest[1389:111828] Hello, World!
blockTest[1389:111828] 定义前 0x1005001b0 0x7fff5fbff7d8
blockTest[1389:111828] in block 0x1005001b0 0x1005003f0
blockTest[1389:111828] lisi
blockTest[1389:111828] 定义后 0x1005001b0 0x7fff5fbff7d8
blockTest[1389:111828] wangwu-->
}

