Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created January 5, 2021 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/8820d384f0d5eee9b27ca2f89566e347 to your computer and use it in GitHub Desktop.
Save sturdysturge/8820d384f0d5eee9b27ca2f89566e347 to your computer and use it in GitHub Desktop.
class DataModel: ObservableObject {
static let url = URL(string: "https://xkcd.com/info.0.json")!
@Published var image: UIImage? = nil
@Published var xkcd: XKCD? = nil
func getImage() {
guard let url = URL(string: xkcd?.img ?? "") else {return}
if let data = try? Data(contentsOf: url) {
self.image = UIImage(data: data)
}
}
init() {
Self.url.getFromPublisher(property: &$xkcd)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment