Skip to content

Instantly share code, notes, and snippets.

@d-date
Created October 26, 2017 07:09
Show Gist options
  • Save d-date/e39fefe625e4e645c51f074504580bb8 to your computer and use it in GitHub Desktop.
Save d-date/e39fefe625e4e645c51f074504580bb8 to your computer and use it in GitHub Desktop.
Discordでわちゃわちゃしてたやつ
let data: Data = """
{
"id": 1,
"url": ""
}
""".data(using: .utf8)!
struct Hoge: Codable {
let id: Int
private let jsonUrl: String?
}
extension Hoge {
enum CodingKeys: String, CodingKey {
case id
case jsonUrl = "url"
}
}
extension Hoge {
var url: URL? {
guard let string = jsonUrl else { return nil }
return URL(string: string)
}
}
let decoder = JSONDecoder()
let hoge = try! decoder.decode(Hoge.self, from: data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment