Swift 如何在此函数中添加其他条件

Swift 如何在此函数中添加其他条件,swift,Swift,如何向函数中添加另一个条件,用于另一个文本字段,以及如何使此代码更简单 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if let text = textField.text { if let floatingLabelTextFiel

如何向函数中添加另一个条件,用于另一个文本字段,以及如何使此代码更简单

   func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        if let text = textField.text {
            if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
                if(text.characters.count < 3 || !text.containsString("@")) {
                    floatingLabelTextField.errorMessage = "Invalid email"
                }
                else {
                    // The error message will only disappear when we reset it to nil or empty string
                    floatingLabelTextField.errorMessage = ""
                }
            }
        }
        return true
    }
func textField(textField:UITextField,shouldChangeCharactersRange:NSRange,replacementString:string)->Bool{
如果let text=textField.text{
如果让floatingLabelTextField=textField作为?SkyFloatingLabelTextField{
if(text.characters.count<3 | |!text.containssString(“@”)){
floatingLabelTextField.errorMessage=“无效电子邮件”
}
否则{
//只有当我们将错误消息重置为nil或空字符串时,错误消息才会消失
floatingLabelTextField.errorMessage=“”
}
}
}
返回真值
}

下面的代码将更简单,更容易添加任何新条件:

func isValidEmail(testStr:String) -> Bool {
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"

    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailTest.evaluate(with: testStr)
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    guard let text = textField.text else{
        return true
    }

    guard let floatingLabelTextField = textField as? SkyFloatingLabelTextField else {
        return true
    }

    if isValidEmail(testStr: text)  {
        floatingLabelTextField.errorMessage = ""
    }else{
        floatingLabelTextField.errorMessage = "Invalid email"
    }
    return true
}
func textField(textField:UITextField,shouldChangeCharactersRange:NSRange,replacementString:string)->Bool
{
如果textField==textField as?SkyFloatingLabelTextField&(!textField.text.containssString(“@”)| | textField.text.characters.count<3)
{
floatingLabelTextField.errorMessage=“无效电子邮件”
}
其他的
{
floatingLabelTextField.errorMessage=“”
}
返回真值
}

您可以首先检查textfield是否为您选择的textfield,在相同条件下,您还可以检查电子邮件验证。

创建一个函数,并将您的textfield引用作为该函数中的参数传递。在该函数中创建您的条件逻辑。将标记分配给textfield textfield 1.tag=1,textfield 2.tag=2,并在shouldChangeCharactersInRange func中检查哪个textfield编辑,如textfield.tag==1{//code for textfiled1}否则{//code for textfield2}这是电子邮件验证吗?您的电子邮件正则表达式现在已经非常过时了:-(IDN和IPv6现在已经成为一种东西。您无法将
UITextField
String
进行比较。
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool

{
    if textField == textField as? SkyFloatingLabelTextField && (!textField.text.containsString("@") || textField.text.characters.count    < 3 )
    {
            floatingLabelTextField.errorMessage = "Invalid email"
    }
    else
    {
        floatingLabelTextField.errorMessage = ""
    }

return true
}