在这一点上已经过了一周,我开始有信心没有我只是缺少一些神奇的语法。为了未来的访问者的利益,这是迄今为止我找到的实现我在这里所问的最佳方式:
typedef struct { char string[32]; } StackString;
StackString foo = {0};
strcpy(foo.string, "foo");
dispatch_block_t block = ^(){
StackString blockFoo = foo;
NSLog(@"Block String: %s", blockFoo.string);
};
for (int i = 0; i < 5; ++i)
{
sprintf(foo.string, "i = %d", i);
NSLog(@"Loop string: %s", foo.string);
block();
}
其输出为:
2013-03-31 11:30:52.778 TestClosure[98968:303] Loop string: i = 0
2013-03-31 11:30:52.780 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.780 TestClosure[98968:303] Loop string: i = 1
2013-03-31 11:30:52.780 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.780 TestClosure[98968:303] Loop string: i = 2
2013-03-31 11:30:52.781 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.781 TestClosure[98968:303] Loop string: i = 3
2013-03-31 11:30:52.782 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.782 TestClosure[98968:303] Loop string: i = 4
2013-03-31 11:30:52.782 TestClosure[98968:303] Block String: foo
这让我相信,包装在一个结构中,固定大小的数组被 const 复制到块闭包中就好了。