ios - 在类中定义的闭包中到达 "self"

标签 ios macos swift cocoa closures

所以我有以下结构并且很好奇这是否可能。

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”

最佳答案

你也可以试试

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
            },

        ]
    }()
}

关于ios - 在类中定义的闭包中到达 "self",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32120812/

相关文章:

ios - 检查是否已在 Apple Watch 上授予 HealthKit 权限的最佳方法?

swift - 以编程方式连接到 VPN 不断要求提供系统钥匙串(keychain)凭据

swift - 编辑模板身份验证 Firebase

swift - SwiftUI View 协议(protocol)中的 EnvironmentObject

ios - 移至可见第二个选项卡的分段 View

ios - 为 SKSpriteNode 添加发光效果

ios - Swift 结构在自定义框架中不可见

ios - 将变量分配给传入 View Controller 的正确方法

objective-c - 重新聚焦时重新加载 NSTableView

python - sphinx-build 安装在 Mac OS-X 中的哪里?