api call in swift

Here's an example of how to make an API call using Alamofire in Swift:

main.swift
import Alamofire

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

Alamofire.request(url, method: .get).responseJSON { response in
    switch response.result {
    case .success(let JSON):
        print("Success with JSON: \(JSON)")
    case .failure(let error):
        print("Request failed with error: \(error)")
    }
}
331 chars
13 lines

This code performs a GET request to https://jsonplaceholder.typicode.com/posts and logs the response to the console. Alamofire is a popular third-party library for handling networking in Swift and provides a convenient and easy-to-use interface for making API calls.

gistlibby LogSnag