我如何比较一个函数,以便我可以分解另一个函数

时间:2017-07-12 23:44:55

标签: swift function

我有一个验证字段的函数。工作正常:

    //MARK: Validate everything before saving to database
    func validateEverything() {
        checkIfFieldsAreEmpty(textField: accountSetupST2.firstNameTextField, alertMessage: "First name field is empty", view: accountSetupST2.nameView)

        checkIfFieldsAreEmpty(textField: accountSetupST2.lastNameTextField, alertMessage: "Last name field is empty", view: accountSetupST2.nameView)

        checkIfFieldsAreEmpty(textField: accountSetupST2.dobTextField, alertMessage: "You DOB can not be empty", view: accountSetupST2.dobView)

        checkIfTextViewsAreEmpty(textView: accountSetupST2.aboutMeTextView, alertMessage: "You need to write something about your self", view: accountSetupST2.aboutMeView)
}

我现在想要实现的是,如果此函数返回true,则触发另一个函数:

 func saveToDatabaseAndGoToHomePage() {
    print("Save and GO Home")

    if validateEverything() != nil {
        print("Good to go")
    } else {
        print("WRONGGGG")
        // add code to database here
    }

}

我已经尝试了几个小时,但仍然无法弄清楚如何去做。 实际上,如果所有字段都有效,则没有错误来触发另一个函数。无论我想要比较什么,总是返回true,即使它完成并且所有文件都经过验证。所以我不能触发另一个功能。

这是checkIfFields为空的函数:

> extension UIViewController {

    //check if text field is empty
    func checkIfFieldsAreEmpty(textField: UITextField, alertMessage: String, view: UIView) {
        if (textField.text?.isEmpty)! {
            let alertEmptyFields = UIAlertController(title: "Warning", message: alertMessage, preferredStyle: .alert)
            let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertEmptyFields.addAction(action)
            self.present(alertEmptyFields, animated: true, completion: nil)
            view.layer.borderWidth = 0.5
            view.layer.borderColor = UIColor.red.cgColor
        } else {
            view.layer.borderWidth = 0
        }

    }

1 个答案:

答案 0 :(得分:1)

您需要修改此方法

extension UIViewController {

//check if text field is empty
func checkIfFieldsAreEmpty(textField: UITextField, alertMessage: String, view: UIView) ->Bool {
    if (textField.text?.isEmpty)! {
        let alertEmptyFields = UIAlertController(title: "Warning", message: alertMessage, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertEmptyFields.addAction(action)
        self.present(alertEmptyFields, animated: true, completion: nil)
        view.layer.borderWidth = 0.5
        view.layer.borderColor = UIColor.red.cgColor
        return false
    } else {
        view.layer.borderWidth = 0
        return true
    }

}

和这一个

    func validateEverything() ->Bool{
           var validTextFields = 0
           validTextFields += checkIfFieldsAreEmpty(textField: accountSetupST2.firstNameTextField, alertMessage: "First name field is empty", view: accountSetupST2.nameView) ? 1 : 0

           validTextFields += checkIfFieldsAreEmpty(textField: accountSetupST2.lastNameTextField, alertMessage: "Last name field is empty", view: accountSetupST2.nameView) ? 1 : 0

           validTextFields += checkIfFieldsAreEmpty(textField: accountSetupST2.dobTextField, alertMessage: "You DOB can not be empty", view: accountSetupST2.dobView) ? 1 : 0

           validTextFields += checkIfTextViewsAreEmpty(textView: accountSetupST2.aboutMeTextView, alertMessage: "You need to write something about your self", view: accountSetupST2.aboutMeView) ? 1 : 0

           return validTextFields == 4
    }

然后你可以这样做

func saveToDatabaseAndGoToHomePage() {
    print("Save and GO Home")

    if validateEverything() {
        print("Good to go")
    } else {
        print("WRONGGGG")
        // add code to database here
    }

}

希望这有帮助