Last active
July 13, 2017 00:55
-
-
Save yutailang0119/33f58104c3a13b53728a885b12e190b4 to your computer and use it in GitHub Desktop.
JSONの中身が配列の時はdecodeのTypeに[T].selfを渡せばいいっぽい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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