Block的测试

Posted yuxiaoyiyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

可变对象没有变化

以上是关于Block的测试的主要内容,如果未能解决你的问题,请参考以下文章