-
-
Save sturdysturge/ba849139ba66dbbe0e4a34a32e730e7a to your computer and use it in GitHub Desktop.
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
import Foundation | |
struct XKCD: Codable { | |
let month: String | |
let num: Int | |
let link, year, news, safeTitle: String | |
let transcript, alt: String | |
let img: String | |
let title, day: String | |
enum CodingKeys: String, CodingKey { | |
case month, num, link, year, news, transcript, | |
alt, img, title, day, safeTitle = "safe_title" | |
} | |
} |
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
import Foundation | |
extension URL { | |
func getFromPublisher<T: Codable>(property: inout Published<T>.Publisher) { | |
URLSession.shared.dataTaskPublisher(for: self) | |
.receive(on: DispatchQueue.main) | |
.map(\.data) | |
.decode(type: T.self, decoder: JSONDecoder()) | |
.assertNoFailure() | |
.assign(to: &property) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment