Skip to content

Instantly share code, notes, and snippets.

@yutailang0119
Last active July 13, 2017 00:55
Show Gist options
  • Save yutailang0119/33f58104c3a13b53728a885b12e190b4 to your computer and use it in GitHub Desktop.
Save yutailang0119/33f58104c3a13b53728a885b12e190b4 to your computer and use it in GitHub Desktop.
JSONの中身が配列の時はdecodeのTypeに[T].selfを渡せばいいっぽい
let data = """
[
{"id": 1, "name": "yutaro"},
{"id": 2, "name": "yutailang"}
]
""".data(using: .utf8)!
struct User: Codable, CustomStringConvertible {
let id: Int
let name: String
var description: String {
return "User id: \(id) name:\(name)"
}
}
do {
let users = try JSONDecoder().decode([User].self, from: data)
users.forEach({ print($0) })
} catch {
print(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment