Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created November 14, 2020 23:40
Show Gist options
  • Save sturdysturge/684d212551d774bdc357e6c265f456ed to your computer and use it in GitHub Desktop.
Save sturdysturge/684d212551d774bdc357e6c265f456ed to your computer and use it in GitHub Desktop.
import SwiftUI
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