gpt4 book ai didi

swift - Plist 与结构不同的字典

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:45 25 4
gpt4 key购买 nike

我有一个 plist,它是一个字典数组。 (根是数组)。每个 dic 肯定有 2 个值:nameicon 作为 Strings。但其中一些具有更多键/值,一些没有

我正在尝试将数组读入结构:

struct Config: Decodable {
private enum CodingKeys: String, CodingKey {
case name, icon
}

let name: String
let icon: String
}



func functionsStruct() -> Config {
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
}
  1. 我读了一本字典,如何阅读字典数组以及如何设置结构。

  2. 如何解决有的dic有key而有的没有?

最佳答案

第一个问题:

我读了一本字典,如何阅读字典数组以及如何设置结构。

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

就这么简单!!!

关于swift - Plist 与结构不同的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com