在 class 中定义的闭包中达到 "self"
Reaching "self" in closure defined in class
所以我有以下结构并且很好奇这是否可行。
class Chip8
{
let foo = 10;
let mapping = [
// CLS (clear the display)
0x00E0: { (argument: Int) -> Void in
// How can I reach self.foo here (self.foo obviously does not work, which is the premise of the question)
},
]
}
这样的事情可能吗?
编辑:
似乎如果我将映射的初始化移动到构造函数,我就可以访问 "self"
我已将映射的初始化移至 init 构造函数,现在我可以从映射中的闭包访问 "self"。
你也可以试试
class Chip8
{
let foo = 10;
lazy var mapping:[Int: (Int)->Void] = {
return [
// CLS (clear the display)
0x00E0: { (argument: Int) -> Void in
// How can I reach self.foo here (self.foo obviously does not work, which is the premise of the question)
self.foo
},
]
}()
}
所以我有以下结构并且很好奇这是否可行。
class Chip8
{
let foo = 10;
let mapping = [
// CLS (clear the display)
0x00E0: { (argument: Int) -> Void in
// How can I reach self.foo here (self.foo obviously does not work, which is the premise of the question)
},
]
}
这样的事情可能吗?
编辑:
似乎如果我将映射的初始化移动到构造函数,我就可以访问 "self"
我已将映射的初始化移至 init 构造函数,现在我可以从映射中的闭包访问 "self"。
你也可以试试
class Chip8
{
let foo = 10;
lazy var mapping:[Int: (Int)->Void] = {
return [
// CLS (clear the display)
0x00E0: { (argument: Int) -> Void in
// How can I reach self.foo here (self.foo obviously does not work, which is the premise of the question)
self.foo
},
]
}()
}