且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift 4 Codable;如何使用单个根级密钥解码对象

更新时间:2022-04-24 00:35:46

您可以使用字典进行解码:用户组合然后提取出用户对象.例如

You could decode using a dictionary: user combination then extract out the user object. e.g.

struct User: Codable {
    let id: Int
    let username: String
}

let decoder = JSONDecoder()
let userDictionary = try decoder.decode([String: User].self, from: jsonData)