Skip to content

Instantly share code, notes, and snippets.

@magicien
Last active December 17, 2018 05:57
Show Gist options
  • Save magicien/208b3574c2b2aa15145773a85863f1b5 to your computer and use it in GitHub Desktop.
Save magicien/208b3574c2b2aa15145773a85863f1b5 to your computer and use it in GitHub Desktop.
import Foundation
let json = """
{
"user_id": 1,
"user_name": "magicien",
"url": "http://darkhorse2.0spec.jp"
}
""".data(using: .utf8)!
struct UserInfo: Codable {
let user_id: Int
let user_name: String
let url: URL
}
let decoder = JSONDecoder()
do {
let userInfo = try decoder.decode(UserInfo.self, from: json)
print(userInfo) // UserInfo(user_id: 1, user_name: "magicien", url: http://darkhorse2.0spec.jp)
} catch DecodingError.keyNotFound(let key, let context) {
print("keyNotFound: \(key): \(context)")
} catch {
print("\(error.localizedDescription)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment