2

I'm familiar with having them with an alamofire function usually after the .response or . responseJSON like in this post or like so:

func checkInLocation (accessToken: String, id: Int, latitude: Double, radius: Double, longitude: Double, completionHandler: String) {

  let headers = [

    "Authorization": "bearer \(accessToken)",
    "Cache-Control": "no-cache",
    "Content-Type": "application/json"
  ]

  let parameters: [String: AnyObject] = [
    "id" : id,
    "latitude": latitude,
    "radius": radius,
    "longitude":longitude,
    "languageCulture": "en"
  ]

  Alamofire.request(.POST, "\(baseApiUrl)members/\(id)/checkin", parameters: parameters, encoding: .JSON, headers: headers)
    .responseJSON(completionHandler: { response in

      if((response.result.value) != nil) {

        let swiftyJsonVar = JSON(response.result.value!)

        print("This is the checkin response:\(swiftyJsonVar)")
      }
  })
}

I'm trying to get it from the call to the function like so:

   checkInLocation(userInfo.sharedInstance.getAccessToken(), id: userInfo.sharedInstance.getMemberID()!, latitude: currentUserLatitude!, radius: 0.3, longitude: currentUserLongitude!, completionHandler: {

      //get JSON response data here

      })
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
  • So.. what is the problem? `func checkInLocation (accessToken: String, id: Int, latitude: Double, radius: Double, longitude: Double, completionHandler: Response -> Void)` – Brandon Jul 21 '16 at 01:36

1 Answers1

1

you can refer below to update your code (check the comment for explaination)

// 1. specify the completion and variables/ data you want to pass. can be more than one with different types (depends on your need)
func checkInLocation (accessToken: String, id: Int, latitude: Double, radius: Double, longitude: Double, completionHandler: (json: JSON) -> Void) {

    let headers = [

        "Authorization": "bearer \(accessToken)",
        "Cache-Control": "no-cache",
        "Content-Type": "application/json"
    ]

    let parameters: [String: AnyObject] = [
        "id" : id,
        "latitude": latitude,
        "radius": radius,
        "longitude":longitude,
        "languageCulture": "en"
    ]

    Alamofire.request(.POST, "\(baseApiUrl)members/\(id)/checkin", parameters: parameters, encoding: .JSON, headers: headers)
        .responseJSON(completionHandler: { response in

            if((response.result.value) != nil) {

                let swiftyJsonVar = JSON(response.result.value!)

                // 2. now pass your variable / result to completion handler
                completionHandler(json: swiftyJsonVar)

                print("This is the checkin response:\(swiftyJsonVar)")
            }
        })
}

and calling the function

checkInLocation(String, id: Int, latitude: Double, radius: Double, longitude: Double) { (json) in
   //
}
xmhafiz
  • 3,482
  • 1
  • 18
  • 26