2017-02-28 11 views
0

iPhoneアプリケーション(Swift 3)で使用するAlamofireのハンドラ関数を設定しようとしています。以下に示すように、ヘルパー関数のセットアップは素晴らしいですが、プロジェクトでどこを使用しているかに応じて、さまざまな成功関数を呼び出す必要があります。 Helper.swiftファイル内渡されたパラメータに応じて、別のViewControllerの関数を呼び出す

ヘルパークラス:

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

} 

そして、私は様々なのViewControllerクラスで様々な時間ごとにこのようなプロジェクト内の様々な他の.swiftファイルでなく、異なるパラメータを使用して、それを呼び出します「非機能タイプ 『メソッド』(別名 『OpaquePointer』)の値を呼び出すことはできませんと言っ:(JSON JSON)それは私にsuccessMethodにエラーメッセージを与えている

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])) 

:でのViewControllerと、状況に応じて私がよ私はそれを正しくしていないことを知っています。踊る。私は単にヘルパー関数を使用しようとしているので、URLリクエストを処理するたびにすべてのURLリクエスト処理アイテムを呼び出す必要はありませんが、成功関数をどのように可変にするかわかりません。 #selectorをパラメータとして使用する必要がありますか?

+0

ブロックを渡すことは、スウィフトyのように思えるでしょう。 –

+0

興味深いですね。コード例で詳しく説明しますか? – user1333394

答えて

0

もう少しインターネットとテスト(フィリップミルズの指揮)の後、私は解決策を見つけました。私は、誰もがそれが役に立つ見つけた場合には掲示しています:

を.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 
       } 
      } 
     } 
    } 
} 

}

そして、私は、他でそれらを使用したいですファイル/ ViewController:

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 
      }) 
関連する問題