Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

46云:Block对象变量捕获 #49

Open
platojobs opened this issue Sep 17, 2021 · 0 comments
Open

46云:Block对象变量捕获 #49

platojobs opened this issue Sep 17, 2021 · 0 comments
Labels
Technology technology

Comments

@platojobs
Copy link
Owner

 FangYanPing * person = [[FangYanPing alloc] init];
            person.age = 10;
            void (^block)(void) = ^{
                NSLog(@"年龄:%ld",person.age);
            };
            person.age = 20;
            block();
            
            NSLog(@"%@",[block class]);

//2021-09-17 18:31:26.103463+0800 xxxApp[3845:180755] 年龄:20
//2021-09-17 18:31:26.103618+0800 xxxApp[3845:180755] __NSMallocBlock__


1、对象的局部变量捕获和基本数据类型有点区别,对象的局部auto变量捕获是指针捕获不是值捕获,所以捕获的内容会受外部变量的影响
2、因为block捕获了auto变量所以blockNSStackBlock,在ARC下将block赋值给__strong指针时会自动将栈上的block复制到堆上,block由NSStackBlock变为NSMallocBlock

@platojobs platojobs added the Technology technology label Sep 17, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Technology technology
Projects
None yet
Development

No branches or pull requests

1 participant