Skip to content

Instantly share code, notes, and snippets.

@ashishkakkad8
Last active August 21, 2022 12:51
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 ashishkakkad8/a3183a0ecfaaee02fc3d1b9c2b81edf4 to your computer and use it in GitHub Desktop.
Save ashishkakkad8/a3183a0ecfaaee02fc3d1b9c2b81edf4 to your computer and use it in GitHub Desktop.
APIHelper in SwiftUI
//
// APIHelper.swift
// APISwiftUI
//
// Created by Kode on 04/08/22.
//
import Foundation
class APIHelper : ObservableObject {
func loadData(completion:@escaping (ContactResult) -> ()) {
guard let url = URL(string: "https://ashishkakkad.com/contacts.json") else {
print("Invalid url...")
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
let result = try! JSONDecoder().decode(ContactResult.self, from: data!)
print(result)
DispatchQueue.main.async {
completion(result)
}
}.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment