块内/外的变量范围

时间:2013-03-06 15:33:18

标签: ios5 objective-c-blocks

我想知道在块内外使用相同名称的变量时变量的范围是多少。一个例子将自己说:

NSSet *test = [NSSet setWithObjects@"Test"];

void (^onComplete)(id) = ^(NSSet *test) {

    // do we see the variable define as an argument of the block or the variable define outside of the block?
    NSSet *test2 = test;

}

NSSet *test3 = test;

这里有没有可能存在的疑虑?

1 个答案:

答案 0 :(得分:2)

局部变量隐藏外部范围。所以在块中,test是参数,而不是外部变量。