2016-07-20 5 views
1

私は、通常は.responseまたは.labelの後ろにalamofire関数を持つことに慣れています。 this post中かそこらなどのようなresponseJSON:AlamoFire用のPOST関数の呼び出しで補完ハンドラを持つ方法は?

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)") 
     } 
    }) 
} 

私はそうのような関数の呼び出しからそれを取得しようとしている:

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

     //get JSON response data here 

     }) 
+0

だから問題は何ですか? (任意のオブジェクト、NSError> - > Void) 'func checkInLocation(accessToken:String、id:Int、緯度:Double、半径:Double、経度:Double、completionHandler: – Brandon

答えて

0

あなたのコードを更新するには以下を参照することができます(コメントをご確認ください、について説明用)

// 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)") 
      } 
     }) 
} 

checkInLocation(String, id: Int, latitude: Double, radius: Double, longitude: Double) { (json) in 
    // 
} 
0123関数を呼び出します
関連する問題