我正在尝试运行此代码:
for spawn : NSDictionary! in spawns {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(waitTime * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
self.spawnSkirmisher(spawn)
})
}
我收到的编译器错误并未指向任何代码行 - 只是构建失败。我可以通过复制spawn
变量来使程序正常工作:
for spawn : NSDictionary! in spawns {
let localSpawn = spawn.copy() as NSDictionary
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(waitTime * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
self.spawnSkirmisher(localSpawn)
})
}
似乎spawn
变量是循环的局部变量,因此它会使闭包/ dispatch_block内的范围失去。
还有另一种处理这种情况的方法吗?好像我正在接近它。谢谢!