2017-02-28 18 views
0

我正在嘗試爲Alamofire設置處理函數,我將在iPhone應用程序中使用它(Swift 3)。我有如下所示的助手功能設置很好,但我希望它調用不同的成功函數,具體取決於我在項目中使用它的位置。根據傳遞的參數在另一個ViewController中調用函數

Helper類中Helper.swift文件:

class Helper { 

    static func toServer(urlString: String, 
         postParams: [String:Any], 
          buttons: [UIButton], 
          messageLabel: UILabel, 
          spinner: UIActivityIndicatorView, 
          successMethod: Method) { 

     // Working State 
     // Hide buttons passed to this function so user can't tap them 
     for button in buttons { 
      button.isHidden = true 
     } 
     // Show the activity indicator 
     spinner.isHidden = false 
     // Hide the message 
     messageLabel.isHidden = true 

     let parameters: Parameters = postParams 
     // Send the http call 
     Alamofire.request(urlString, method: .post, parameters: parameters).validate().responseJSON { response in 
      switch response.result { 
      case .failure: 
       //print(error) 
       // Hide the activity indicator 
       spinner.isHidden = true 
       // Show error message 
       messageLabel.text = "No Internet. Try again".uppercased() 
       messageLabel.isHidden = false 
       // Show buttons passed to this function so user can tap them again 
       for button in buttons { 
        button.isHidden = false 
       } 
      case .success: 
       if let json = response.result.value as? [String: Any], 
       let code = json["code"] as? Int, 
       let response = json["response"] as? String { 
        if code != 1 { 
         // Server (json data) didn't return success 
         // Hide the activity indicator 
         spinner.isHidden = true 
         // Show unsuccessful data entry by the user 
         messageLabel.text = response.uppercased() 
         messageLabel.isHidden = false 
         // Show buttons passed to this function so user can tap on them 
         for button in buttons { 
          button.isHidden = false 
         } 
        } else { 
         // Server (json data) returned success (a 1) 
         // Call the success function passed to this function as a parameter and send it the json data sent by the server 
         successMethod(json: json) 
        } 
       } else { 
        // Print the response for debugging 
        print(response) 
        // Hide the activity indicator 
        spinner.isHidden = true 
        // Show error message to the user 
        messageLabel.text = "App error \(#line)".uppercased() 
        messageLabel.isHidden = false 
        // Show buttons passed to this function so user can tap them 
        for button in buttons { 
         button.isHidden = false 
        } 
       } 
      } 
     } 
    } 

} 

然後,我把它叫做項目中的各種視圖控制器類不同的時間以各種其他.swift文件,每次都像這樣,但是使用不同的參數根據到ViewController和情況我在:

Helper.toServer("https://example.com/page.php", 
postParams: ["email":email.text!.trimmingCharacters(in:NSCharacterSet.whitespacesAndNewlines)], 
buttons: [registerButton], 
messageLabel: message, 
spinner: spinner, 
successMethod: self.successFunction(json: [String:Any])) 

它給我上successMethod錯誤消息(JSON:JSON)說「無法調用非功能型‘方法’(又名‘OpaquePointer’)的值我知道我做得不對,請給我一個我跳舞。我只是試圖使用一個輔助函數,所以我不必每次都要調用URL請求時調用所有的URL請求處理項目,但我無法弄清楚如何讓成功函數變化。我需要使用#selector作爲參數嗎?

+0

塊中的傳遞會像雨燕-Y的事情。 –

+0

聽起來很有趣。請仔細閱讀代碼示例? – user1333394

回答

0

經過更多的內部測​​試和測試(以及Phillip Mills的指導)後,我找到了一個解決方案。我萬一有人張貼發現它有用:

在.swift文件:

類助手{

class func toServer(_ urlString: String, 
         postParams: [String:Any], 
         buttons: [UIButton], 
         messageLabel: UILabel, 
         spinner: UIActivityIndicatorView, 
         success:@escaping ([String:Any]) -> Void) { 

    // Working State 
    // Hide buttons passed to this function so user can't tap them 
    for button in buttons { 
     button.isHidden = true 
    } 
    // Show the activity indicator 
    spinner.isHidden = false 
    // Hide the message 
    messageLabel.isHidden = true 

    let parameters: Parameters = postParams 
    // Send the http call 
    Alamofire.request(urlString, method: .post, parameters: parameters).validate().responseJSON { response in 
     switch response.result { 
     case .failure: 
      //print(error) 
      // Hide the activity indicator 
      spinner.isHidden = true 
      // Show error message 
      messageLabel.text = "No Internet. Try again".uppercased() 
      messageLabel.isHidden = false 
      // Show buttons passed to this function so user can tap them again 
      for button in buttons { 
       button.isHidden = false 
      } 
     case .success: 
      if let json = response.result.value as? [String: Any], 
       let code = json["code"] as? Int, 
       let message = json["response"] as? String { 
       if code != 1 { 
        // Server (json data) didn't return success 
        // Hide the activity indicator 
        spinner.isHidden = true 
        // Show unsuccessful data entry by the user 
        messageLabel.text = message.uppercased() 
        messageLabel.isHidden = false 
        // Show buttons passed to this function so user can tap on them 
        for button in buttons { 
         button.isHidden = false 
        } 
       } else { 
        // Server (json data) returned success (a 1) 
        let json = response.result.value as? [String: Any] 
        success(json!) 
       } 
      } else { 
       // Print the response for debugging 
       print(response) 
       // Hide the activity indicator 
       spinner.isHidden = true 
       // Show error message to the user 
       messageLabel.text = "App error \(#line)".uppercased() 
       messageLabel.isHidden = false 
       // Show buttons passed to this function so user can tap them 
       for button in buttons { 
        button.isHidden = false 
       } 
      } 
     } 
    } 
} 

}

而當我想以任何其它使用它們文件/視圖控制器:

Helper.toServer("https://example.com/page", postParams: ["email":email.text!, "password":password.text!], buttons: [registerButton, anotherButton], messageLabel: message, spinner: spinner, success: { 
       (JSONResponse) -> Void in 
       print(JSONResponse) 

       // Do stuff with JSON result now that it's a success 
      }) 
相關問題