API Request with Alamofire September 5, 2016

            
      // An example of making an API call in Swift 3 with Alamofire.
      // Assumes you've already included Alamofire in your project.
      // See also: Guide to installing Alamofire with Cocoapods.

      class APIController: NSObject {
          
          func downloadDataFromAPI(withUrl url: String, completionHandler: @escaping (NSDictionary?, NSError?) -> ()) {
              
              networkRequest(withUrl: url, completionHandler: completionHandler)
          }
          
          func networkRequest(withUrl url: String, completionHandler: @escaping (NSDictionary?, NSError?) -> ()) {
              
              Alamofire.request(url).validate().responseJSON { response in
                  switch response.result {
                      
                  case .success(let value):
                      completionHandler(value as? NSDictionary, nil)
                      
                  case .failure(let error):
                      completionHandler(nil, error as NSError?)
                  }
              }
          }
      }

      // To make a call to an API, instantiate the APIController class
      let API = APIController()

      // Specify the URL of the endpoint you're hitting
      let urlForAPIRequest : String = "http://api.example.com/"


      // Make the call
      API.downloadDataFromAPI(urlForAPIRequest) { responseObject, error in
          
          // ...and when the data comes back asynronously use the responseObject
          let json = responseObject? as! Dictionary
          return
          
      }