Making API calls and parsing JSON objects

in 3 simple steps

MORE TUTORIALS

Installing Alamofire and SwiftyJSON

Create a Podfile and replace the text with the code below. And run a Pod Install

platform :ios, '9.0'
use_frameworks!

target 'AlamofireSwiftyJSONSample' do
pod 'Alamofire'
pod 'SwiftyJSON'
end

Step 1 of 3

Import frameworks

Create a Podfile and replace the text with the code below. And run a Pod Install

import Alamofire
import SwiftyJSON

Step 2 of 3

Make an API Call and Parse Data

Paste the code below in viewDidLoad() method of your ViewController class

Alamofire.request("https://jsonplaceholder.typicode.com/posts/1", method: .get).validate().responseJSON { response in
   switch response.result {
        case .success(let value):
           let json = JSON(value)
           print(json)
           print("User ID: " + json["userId"])
           print("Title: " + json["title"])
           print("Body: " + json["body"])
       case .failure(let error):
           print(error)
   }
}

Step 3 of 3