//输出10 int x = 10; void(^function)(void) = ^(void) { printf("-----> %d",x); }; x = 12; function();
//输出12 int __block x = 10; void(^function)(void) = ^(void) { printf("-----> %d",x); }; x = 12; function();
上面两个换成NSString一样是这种情况,带__block才会输出后面配置的新值。
//输出ab int __block x = 10; NSMutableString *y = [NSMutableString stringWithString:@"a"]; void(^function)(void) = ^(void) { printf("-----> %d\n",x); printf("++++>%s",[y UTF8String]); }; x = 12; [y appendString:@"b"]; function();
可变对象没有问题
//正常输出 const char * text = "hello"; void(^function)(void) = ^(void) { printf("-----> %d\n",x); printf("++++>%s",[y UTF8String]); [items addObject:@(10)]; printf("%c",text[2]); };
//数组没有实现对数组的截获,无法在Block里面使用数组 const char text[] = "hello"; void(^function)(void) = ^(void) { printf("-----> %d\n",x); printf("++++>%s",[y UTF8String]); [items addObject:@(10)]; printf("%c",text[2]); };