Created
October 26, 2017 07:09
-
-
Save d-date/e39fefe625e4e645c51f074504580bb8 to your computer and use it in GitHub Desktop.
Discordでわちゃわちゃしてたやつ
This file contains hidden or 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: 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