create a network call in swift

To create a network call in Swift, we typically use a networking library like Alamofire to simplify the process. Here's an example of how to make a GET request using Alamofire:

main.swift
import Alamofire

let url = "https://jsonplaceholder.typicode.com/todos"

AF.request(url).responseJSON { response in
    switch response.result {
    case .success(let json):
        print("JSON: \(json)")
    case .failure(let error):
        print("Error: \(error)")
    }
}
277 chars
13 lines

In this example, we're making a GET request to https://jsonplaceholder.typicode.com/todos using a GET method provided by Alamofire. When the request is completed, the response is passed to a closure that handles success and failure cases.

Note that this is a very basic example, and there are many other options available for customizing your network requests, including adding headers, HTTP methods, and parameters.

gistlibby LogSnag