根据传递的参数

时间:2017-02-28 20:55:43

标签: ios swift alamofire

我正在尝试为Alamofire设置一个处理函数,我将在我的iPhone应用程序(Swift 3)中使用它。我有如下所示的辅助函数设置,但我希望它根据我在项目中使用它的位置调用不同的成功函数。

Helper.swift文件中的Helper类:

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类中调用它,例如这个,但每次根据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作为参数吗?

1 个答案:

答案 0 :(得分:0)

经过多次Interneting和测试(以及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
                }
            }
        }
    }
}

}

当我想在任何其他文件/ 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
            })