在block内部更改外部的变量

18-08-13 12:01 字数 343 阅读 1831 已编辑
#import <Foundation/Foundation.h>
void myBLock() {
    __block int num = 5;
    void (^myBlockInternal)(void) = ^{
        num = 6;
        NSLog(@"%d", num);
    };
    myBlockInternal();
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        myBLock();
    }
    return 0;
}

输出

2018-08-13 12:00:14.264296+0800 Block[43795:1488802] 6
2018-08-13 12:00:14.264494+0800 Block[43795:1488802] Hello, World!
Program ended with exit code: 0
0人点赞>
关注 收藏 改进 举报
0 条评论
排序方式 时间 投票
快来抢占一楼吧
请登录后发表评论
站长 @ 十七度
文章
380
粉丝
23
喜欢
191
收藏
31
排名 : 1
访问 : 130.05万
私信