- (void)viewDidLoad {
[super viewDidLoad];
auto int a = 5;//自动变量:过作用域就销毁(值传递)
static int b = 5;//静态变量(指针传递)
//block会对局部变量进行捕获、全局变量不会捕获
self.block = ^() {
NSLog(@"a=%d b=%d",a,b);
};
a = 20;
b = 20;
self.block();
}
2018-06-02 16:58:57.752234+0800 testaaa[7833:742834] a=5 b=20
#import "person.h"
@implementation person
-(void)log
{
//默认传入
//void eat (person * self,SEL _cmd) {
// }
//参数是局部变量,依然会被捕获
void(^block)(void) = ^{
//self为局部变量
NSLog(@"%@",self);
//_name的本质是self->_name
NSLog(@"%@",_name);
};
block();
}
void eat (person * self,SEL _cmd) {
}
@end