Swift 如何在文本字段中添加不允许的空格

Swift 如何在文本字段中添加不允许的空格,swift,xcode,namespaces,uitextfield,lowercase,Swift,Xcode,Namespaces,Uitextfield,Lowercase,这是我的代码,我希望用户名不能有空格和小写 示例(wizkhalifa) 您需要检查两件事,您可以在尝试创建用户和/或用户键入时将该检查放入调用的方法中 首先检查空格: if let text = UserNameField.text { if text.components(separatedBy: " ").count > 1 && text.lowercased() != textField.text {// add your other tests on t

这是我的代码,我希望用户名不能有空格和小写 示例(wizkhalifa)


您需要检查两件事,您可以在尝试创建用户和/或用户键入时将该检查放入调用的方法中

首先检查空格:

if let text = UserNameField.text {
    if text.components(separatedBy: " ").count > 1 && text.lowercased() != textField.text {// add your other tests on this line
        // the text is in lowercase without space character and your tests pass
        Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
             // check if there is an error
        })
    } else {
        AlertController.showAlert(self, titel: "Error", message: " Fill all fields \nUser exists  ")

}
以下是OP的版本及其代码:

if  UserNameField.text != nil, emailField.text != nil, passwordField.text != nil, rePassword.text != nil && passwordField.text == rePassword.text && UserNameField.text.components(separatedBy: " ").count <= 1 && UserNameField.text.lowercased() == UserNameField.text {
    Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
        if error != nil {
            AlertController.showAlert(self, title: "Error", message: " Fill all fields \nUser exists  ")
            return
        } else {
            // all good
        }
    })
} else {
    AlertController.showAlert(self, title: "Error", message: " Fill all fields \nUser exists  ")
}

如果UserNameField.text!=无,emailField.text!=零,passwordField.text!=零,rePassword.text!=nil&&passwordField.text==rePassword.text&&UserNameField.text.components(分隔符:“”)。count您需要检查两件事,您可以在尝试创建用户和/或用户键入时将该检查放入调用的方法中

首先检查空格:

if let text = UserNameField.text {
    if text.components(separatedBy: " ").count > 1 && text.lowercased() != textField.text {// add your other tests on this line
        // the text is in lowercase without space character and your tests pass
        Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
             // check if there is an error
        })
    } else {
        AlertController.showAlert(self, titel: "Error", message: " Fill all fields \nUser exists  ")

}
以下是OP的版本及其代码:

if  UserNameField.text != nil, emailField.text != nil, passwordField.text != nil, rePassword.text != nil && passwordField.text == rePassword.text && UserNameField.text.components(separatedBy: " ").count <= 1 && UserNameField.text.lowercased() == UserNameField.text {
    Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
        if error != nil {
            AlertController.showAlert(self, title: "Error", message: " Fill all fields \nUser exists  ")
            return
        } else {
            // all good
        }
    })
} else {
    AlertController.showAlert(self, title: "Error", message: " Fill all fields \nUser exists  ")
}

如果UserNameField.text!=无,emailField.text!=零,passwordField.text!=零,rePassword.text!=nil&&passwordField.text==rePassword.text&&UserNameField.text.components(分隔符:“”)。计数对不起,您能给我一个实例吗?请告诉我如何将这些行附加到一个textfield我用您的变量名编辑了我的答案,只需使用该代码。出现的错误是由于可选类型,即?表示该值可以是nil,也可以是定义类型的任何值。你必须打开它:我在回答中编辑了代码。你可能应该去看看关于如何开始在swift中开发的教程(就像苹果公司的这篇),还有文档ibooksI编辑了我的答案,检查代码。如果测试通过,则调用firebase方法;如果测试不通过,则调用警报。在学习如何使用第三方库和其他东西之前,你真的应该先看看关于编程的基本教程,条件语句是你编程的第一步。在进行类似的项目之前,请先学习基础开发。对不起,你能给我一个实例吗?请告诉我如何将这些行附加到一个文本字段我用你的变量名编辑了我的答案,只需使用该代码。你的错误是由于可选类型,即?表示该值可以是nil,也可以是定义类型的任何值。你必须打开它:我在回答中编辑了代码。你可能应该去看看关于如何开始在swift中开发的教程(就像苹果公司的这篇),还有文档ibooksI编辑了我的答案,检查代码。如果测试通过,则调用firebase方法;如果测试不通过,则调用警报。在学习如何使用第三方库和其他东西之前,你真的应该先看看关于编程的基本教程,条件语句是你编程的第一步。在参加这样的项目之前,请先学习基础开发。