Created
July 11, 2020 13:04
Star
You must be signed in to star a gist
さっきの https://anoqode.com/question?id=9dd1d9f9-bba9-4cde-b331-1ab78b155b01 ですけど、RawRepresentableなstructを噛ませておくと、失敗した文字列も失わずに済むので好きです #swiftzoomin #CodePiece
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 jsonData = """ | |
[ | |
"https://www.apple.com", | |
"https://swift.org/blog/", | |
"https://github.com/apple/swift/releases/tag/swift 5.2.4-RELEASE" | |
] | |
""" | |
.data(using: .utf8)! | |
struct SafeURL: RawRepresentable, Decodable { | |
let rawValue: String | |
var url: URL? { URL(string: rawValue) } | |
} | |
let urls = try JSONDecoder().decode([SafeURL].self, from: jsonData) | |
urls.map(\.url) | |
// [https://www.apple.com, https://swift.org/blog/, nil] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment