Junior — Middle
How is network interaction implemented in your project?
sobes.tech AI
Answer from AI
In my iOS project, network interaction is implemented using URLSession, which provides an API for performing HTTP requests.
For convenience and reusability, I created a network layer with abstraction that handles request creation, response processing, and errors. Codable is also used for JSON parsing.
Example of a simple request:
func fetchUsers(completion: @escaping ([User]?, Error?) -> Void) {
let url = URL(string: "https://api.example.com/users")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
completion(nil, error)
return
}
do {
let users = try JSONDecoder().decode([User].self, from: data)
completion(users, nil)
} catch {
completion(nil, error)
}
}.resume()
}
For more complex tasks, third-party libraries like Alamofire are used for convenient request building and response handling.