一、block的三种形式
堆block:
在堆上的block,
用__block修饰的外部参数,会将block拷贝(copy修饰)到栈上,从栈复制到堆并被block持有
栈block:
在栈上的block
未copy到堆的block,有外部参数
有值域的问题,用__block将block复制到堆解决值域的问题,从而解决值域问题
全局block:
在静态区的block
不使用外部变量的block,或值用static修饰的,是全局block
二、block的copy
堆block:引用计算+1
栈block:会copy到堆block
全局block:啥也不做
总结
static NSInteger num1 = 1;
NSInteger num2 = 1;
//static NSMutableArray *marray1 = [NSMutableArray arrayWithArray:@[@1]];
//NSMutableArray static *marray2 = [NSMutableArray arrayWithArray:@[@1]];
//static NSArray *array1 = @[@1];
//NSArray static *marray2 = @[@1];
static NSString *string1 = @"1";
NSString static *string2 = @"1";
int test1(){
NSInteger num3 = 1;
static NSInteger num4 = 1;
__block NSInteger num5 = 1;
NSString *string3 = @"1";
static NSString *string4 = @"1";
__block NSString *string5 = @"1";
NSMutableArray *marray3 = [NSMutableArray arrayWithArray:@[@1]];
// static NSMutableArray *marray4 = [NSMutableArray arrayWithArray:@[@1]];
__block NSMutableArray *marray5 = [NSMutableArray arrayWithArray:@[@1]];
void(^block)(void) = ^{
NSLog(@"NSInteger");
NSLog(@"%zd",num1);///全局静态变量
NSLog(@"%zd",num2);//全局变量
NSLog(@"%zd",num3);//局部变量
NSLog(@"%zd",num4);//静态变量
NSLog(@"%zd",num5);//__block修饰变量
NSLog(@"NSMutableArray");
// NSLog(@"%@",marray1);
// NSLog(@"%@",marray2);
NSLog(@"%@",marray3);
// NSLog(@"%@",marray4);
NSLog(@"%@",marray5);
NSLog(@"NSString");
NSLog(@"%@",string1);
NSLog(@"%@",string2);
NSLog(@"%@",string3);
NSLog(@"%@",string4);
NSLog(@"%@",string5);
};
NSLog(@"---------------------初始化---------------------------");
block();
num1 = 2;
num2 = 2;
num3 = 2;
num4 = 2;
num5 = 2;
string1 = @"2";
string2 = @"2";
string3 = @"2";
string4 = @"2";
string5 = @"2";
[marray3 addObject:@2];
[marray5 addObject:@2];
NSLog(@"---------------------赋值---------------------------");
block();
string1 = nil;
string2 = nil;
string3 = nil;
string4 = nil;
string5 = nil;
marray3 = nil;
marray5 = nil;
NSLog(@"---------------------赋空---------------------------");
block();
return 0;
}
2021-01-11 17:44:20.312008+0800 SEEProxy[92404:4949653] ---------------------初始化---------------------------
2021-01-11 17:44:20.312503+0800 SEEProxy[92404:4949653] NSInteger
2021-01-11 17:44:20.312571+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.312647+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.312712+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.312774+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.312837+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.312894+0800 SEEProxy[92404:4949653] NSMutableArray
2021-01-11 17:44:20.313099+0800 SEEProxy[92404:4949653] (
1
)
2021-01-11 17:44:20.313227+0800 SEEProxy[92404:4949653] (
1
)
2021-01-11 17:44:20.313294+0800 SEEProxy[92404:4949653] NSString
2021-01-11 17:44:20.313454+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.313617+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.313804+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.314039+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.314218+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.314432+0800 SEEProxy[92404:4949653] ---------------------赋值---------------------------
2021-01-11 17:44:20.314586+0800 SEEProxy[92404:4949653] NSInteger
2021-01-11 17:44:20.314768+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.319000+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.319072+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.319124+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.319171+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.319219+0800 SEEProxy[92404:4949653] NSMutableArray
2021-01-11 17:44:20.319324+0800 SEEProxy[92404:4949653] (
1,
2
)
2021-01-11 17:44:20.319385+0800 SEEProxy[92404:4949653] (
1,
2
)
2021-01-11 17:44:20.319520+0800 SEEProxy[92404:4949653] NSString
2021-01-11 17:44:20.319754+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.319991+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.320205+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.320428+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.320654+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.320873+0800 SEEProxy[92404:4949653] ---------------------赋空---------------------------
2021-01-11 17:44:20.321086+0800 SEEProxy[92404:4949653] NSInteger
2021-01-11 17:44:20.321251+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.321405+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.321584+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.321769+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.321949+0800 SEEProxy[92404:4949653] 2
2021-01-11 17:44:20.322128+0800 SEEProxy[92404:4949653] NSMutableArray
2021-01-11 17:44:20.322410+0800 SEEProxy[92404:4949653] (
1,
2
)
2021-01-11 17:44:20.322596+0800 SEEProxy[92404:4949653] (null)
2021-01-11 17:44:20.322786+0800 SEEProxy[92404:4949653] NSString
2021-01-11 17:44:20.323016+0800 SEEProxy[92404:4949653] (null)
2021-01-11 17:44:20.323245+0800 SEEProxy[92404:4949653] (null)
2021-01-11 17:44:20.323433+0800 SEEProxy[92404:4949653] 1
2021-01-11 17:44:20.323617+0800 SEEProxy[92404:4949653] (null)
2021-01-11 17:44:20.323812+0800 SEEProxy[92404:4949653] (null)
2021-01-11 17:44:20.362871+0800 SEEProxy[92404:4949653] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
2021-01-11 17:44:20.457284+0800 SEEProxy[92404:4949653] 我是美人鱼
2021-01-11 17:44:20.457405+0800 SEEProxy[92404:4949653] 我在游泳
以上可看出,除了栈block,其他block都会随着值变化而变化
网友评论