2016-06-14 4 views
1

私には1つの問題があります。私はalamofireとそれを返す価値reuestingです。 alamofireでは、返されたjsonを私のカスタムクラス、その仕事のgoogに変換します。しかし、私はalamofireボディのこの変換されたクラスを使用しているとき、それはゼロの値を返しました。 私は、jsonの値を変換し、クラスにappendするalamofireにブレークポイントを設定し、値を持ち、それをclass.Butに入れました。どうして?alamofire get request null値を返す

答えて

0

これは、alamofireがzD.detail.append(p)に何かを追加する前にデータを非同期に読み込むために起こります。 ロードが完了するまで待機するプログラムの完了ハンドラを追加します。

その後
//here you call authenticateUser with a closure that prints responseObject 
API().authenticateUser{ (responseObject, error) in 
    println(responseObject) 
} 

:詳細については

//authenticateUser receives your closure as a parameter 
func authenticateUser(completionHandler: (responseObject: String?, error: NSError?) ->()) { 
    //it passes your closure to makeAuthenticateUserCall 
    makeAuthenticateUserCall(completionHandler) 
} 

//makeAuthenticateUserCall receives your closure 
func makeAuthenticateUserCall(completionHandler: (responseObject: String?, 
error: NSError?) ->()) { 
    Alamofire.request(.GET, loginUrlString) 
     .authenticate(user: "a", password: "b") 
     //here you pass a new closure to the responseString method 
     .responseString { request, response, responseString, responseError in 
      //in this closure body you call your completionHandler closure with the 
      //parameters passed by responseString and your code gets executed 
      //(that in your case just prints the responseObject) 
      completionHandler(responseObject: responseString as String!, error: responseError) 
    } 
} 

は、ドキュメントを読んで:Swift Closures

+0

を、それは、製品= JSON(値)を聞かせてJSONをダウンロードし、すでにある –

+0

うんだけでなく、あなたがコードに伝える必要がありますがalamofireがデータをロードするまで何も追加しないでください。それが終わったら、それを追加してデータをリロードすることができます。 – Dershowitz123

+0

のように: func getZakazDetail(orderID:Int、tableView:UITableView、スピナー:UIActivityIndi​​catorView、完了:(JSON) - >())商品= JSON(値) 完了(製品) –