ios - swift plist 解码器全局 Spritekit

标签 ios swift sprite-kit plist

我想要我的 plist 中的字典,我可以在整个游戏场景中使用它。但是在我的解决方案中,我总是必须调用 parseConfig 函数才能从 plist 中获取字典。

struct Config: Decodable {
    private enum CodingKeys: String, CodingKey {
        case zPositions, enemy, player
    }

    let zPositions: [String:Double]
    let enemy: [String:[String:Double]]
    let player: [String:[String:Double]]
}

func parseConfig() -> Config {
    let url = Bundle.main.url(forResource: "Config", withExtension: "plist")!
    let data = try! Data(contentsOf: url)
    let decoder = PropertyListDecoder()
    return try! decoder.decode(Config.self, from: data)
}

最佳答案

尝试创建惰性属性。

类似于:- 如果使用 gloabaly

var parsedConfig: Config  = {
    let url = Bundle.main.url(forResource: "Config", withExtension: "plist")!
    let data = try! Data(contentsOf: url)
    let decoder = PropertyListDecoder()
    return try! decoder.decode(Config.self, from: data)
}()

如果在类中,在var之前添加lazy

关于ios - swift plist 解码器全局 Spritekit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52908684/

相关文章:

ios - IOS 中带有确认对话框的 Segue

ios - 将为 4 英寸屏幕开发的 iOS 应用程序适配到 3.5 英寸屏幕

swift - deinit 末尾的 Defer 语句会产生警告

ios - 麦克风播放的音频断断续续,听起来像是空气吹入麦克风

ios - 从 GameScene 设置自定义 SKSpriteNode 的位置

ios - 在执行 SKAction moveTo 时跟踪 SKSpriteNode 的位置

ios - Transform(Set SCNNode on sufrace ) SCNNode 与 SCNMaterial 不改变 SCNMaterial 的方向包含图像

iOS Spotify 歌曲链接

ios - 自动在导航栏中添加 "back"按钮

ios - 更改 UILabel 文本仅适用于 viewDidLoad()