Block的测试

//输出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]);
    };

 

转载于:https://www.cnblogs.com/yuxiaoyiyou/p/11214865.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值