2016-04-08 16 views
0

私はAlamofireを使用してAPIに接続し、JSONオブジェクトを元に戻し、変数に代入してから表示しようとしています。問題は、変数がサーバーから値を戻す前に表示しようとしているため、変数が空であるということです。機能を実行する前にSwiftで変数を待つ方法は?

ここに私のNetworking.swiftファイルです:

class Networking { 

class func postPurchase() { 

    let parameters = [ 
     "login_user": "[email protected]", 
     "login_password": "p0q3t4", 
     "item_id": 5, 
     "machine_token": "/HyZyq2FgU4RONnDlzPXWA==", 
     "amount": 1 
    ] 

    Alamofire.request(.POST, "http://poqeta.herokuapp.com/api/v1/purchases/add_item", parameters: parameters, encoding: .JSON) 
     .responseData { response in 
      print(response.request) 
      print(response.response) 
      print(response.result) 
    } 
} 

class func confirmPurchase() -> String { 
    var token:String = " " 

    Alamofire.request(.POST, "http://poqeta.herokuapp.com/api/v1/purchases/purchase", parameters: ["login_user": "[email protected]", "login_password": "p0q3t4"]) 
     .responseJSON { response in 
      switch response.result { 
      case .Success(let data): 
       let json = JSON(data) 
       let dispenseToken:String = json["token"].stringValue 
       print(dispenseToken) 
       token = dispenseToken 
      case .Failure(let error): 
       print("Request failed with error: \(error)") 
       return 
      } 
    } 
    return token 
} 

そして、ここでは、変数を取得し、(PopUpViewControllerSwiftを使用して)それを表示しようとしている機能です。任意の提案を事前に

@IBAction func showPopUp(sender: AnyObject) { 

    var purchase_token:String = " " 

    Networking.postPurchase() 
    purchase_token = Networking.confirmPurchase() 

    let bundle = NSBundle(forClass: PopUpViewControllerSwift.self) 
    if (UIDevice.currentDevice().userInterfaceIdiom == .Pad) 
    { 
     self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController_iPad", bundle: bundle) 
     self.popViewController.title = "Purchase Complete" 
     self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
    } else 
    { 
     if UIScreen.mainScreen().bounds.size.width > 320 { 
      if UIScreen.mainScreen().scale == 3 { 
       self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController_iPhone6Plus", bundle: bundle) 
       self.popViewController.title = "Purchase Complete" 
       self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
      } else { 
       self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController_iPhone6", bundle: bundle) 
       self.popViewController.title = "Purchase Complete" 
       self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
      } 
     } else { 
      self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController", bundle: bundle) 
      self.popViewController.title = "Purchase Complete" 
      self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
     } 
    } 
} 

ありがとう!

+3

あなたが完了ハンドラが含まれるようにconfirmPurchaseを変更する必要があります。ここで提案似た何か:http://stackoverflow.com/questions/ 36416163/include-a-return-handler-in-async-call-in-swift/36417000?noredirect = 1#コメント60583499_36417000 – Shripada

答えて

2

postpurchase & confirmPurchaseは、acsync関数です。戻り値は使用しないでください。 試してみてください。

class func postPurchase(handleComplete:((isOK:Bool)->())) { 

    let parameters = [ 
     "login_user": "[email protected]", 
     "login_password": "p0q3t4", 
     "item_id": 5, 
     "machine_token": "/HyZyq2FgU4RONnDlzPXWA==", 
     "amount": 1 
    ] 



    Alamofire.request(.POST, "http://poqeta.herokuapp.com/api/v1/purchases/add_item", parameters: parameters, encoding: .JSON) 
      .responseData { response in 
       print(response.request) 
       print(response.response) 
       print(response.result) 
       // check request success or failse 
//    if success 
       handleComplete(isOK: true) 
//    else 
//    handleComplete(isOK: false) 
     } 
    } 

class func confirmPurchase(handleComplete:((token:String?)->())){ 
    var token:String = " " 

    Alamofire.request(.POST, "http://poqeta.herokuapp.com/api/v1/purchases/purchase", parameters: ["login_user": "[email protected]", "login_password": "p0q3t4"]) 
     .responseJSON { response in 
      switch response.result { 
      case .Success(let data): 
       let json = JSON(data) 
       let dispenseToken:String = json["token"].stringValue 
       print(dispenseToken) 
       token = dispenseToken 
       handleComplete(token: token) 
      case .Failure(let error): 
       print("Request failed with error: \(error)") 
       handleComplete(token: nil) 
      } 
    } 
} 

し、それを使用します。

@IBAction func showPopUp(sender: AnyObject) { 

    var purchase_token:String = " " 

    Networking.postPurchase { (isOK) in 
     if isOK{ 
      Networking.confirmPurchase({ (token) in 
       if let tok = token{ 
        purchase_token = tok 

        let bundle = NSBundle(forClass: PopUpViewControllerSwift.self) 
        if (UIDevice.currentDevice().userInterfaceIdiom == .Pad) 
        { 
         self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController_iPad", bundle: bundle) 
         self.popViewController.title = "Purchase Complete" 
         self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
        } else 
        { 
         if UIScreen.mainScreen().bounds.size.width > 320 { 
          if UIScreen.mainScreen().scale == 3 { 
           self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController_iPhone6Plus", bundle: bundle) 
           self.popViewController.title = "Purchase Complete" 
           self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
          } else { 
           self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController_iPhone6", bundle: bundle) 
           self.popViewController.title = "Purchase Complete" 
           self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
          } 
         } else { 
          self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController", bundle: bundle) 
          self.popViewController.title = "Purchase Complete" 
          self.popViewController.showInView(self.view, withImage: nil, withMessage: "Your purchase token is " + purchase_token, animated: true) 
         } 
        } 

       }else{ 
        // false --> do st 
       } 
      }) 
     } else{ 
      // do st 
     } 
    } 

} 
+0

恐ろしいです!ありがとう、それは働いた。しかし、それは非常にゆっくりと情報を引き出しています。要求をスピードアップするためにできることは何ですか? さらに、このプロセスについてはどこで読むことができますか?コードが機能したので、何が起こっているのかをよりよく理解したい – Kaidao

関連する問題