Last active
August 21, 2022 12:51
-
-
Save ashishkakkad8/a3183a0ecfaaee02fc3d1b9c2b81edf4 to your computer and use it in GitHub Desktop.
APIHelper in SwiftUI
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
// | |
// 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