gpt4 book ai didi

ios - 如何改进 Swift/Xcode iOS 应用程序中文本字段的控制语句?

转载 作者:行者123 更新时间:2023-11-29 05:51:18 25 4
gpt4 key购买 nike

我正在为 iPhone 设置一个新应用程序,并且我想仅在文本字段不为空时将用户输入信息添加到我的数据库中。

有 4 个文本字段,但只需填写其中一个即可更新我的数据库。

Swift 和 iOS 编码对我来说是新的。

这是我想要改进的代码:

@IBAction func confirmButtonWasPressed(_ sender: UIButton) {
print("Redirection to confirmation screen.")
if confirmpasswordTextField.text != "" {
print("Updating informations.")

// Read values from text fields
let userName = newusernameTextField.text
let userMail = newmailTextField.text
let userPhone = newphoneTextField.text
let userPassword = newpasswordTextField.text

if userName?.isEmpty ?? true {
print("textField is empty")
} else {
print("textField has some text")
}

if userMail?.isEmpty ?? true {
print("textField is empty")
} else {
print("textField has some text")
}

if userPhone?.isEmpty ?? true {
print("textField is empty")
} else {
print("textField has some text")
}

if userPassword?.isEmpty ?? true {
print("textField is empty")
} else {
print("textField has some text")
}

}else{
print("Error: Please enter your password.")
}
}

我想减少这个功能并获取输入(如果有)。

我可以用这段代码来做到这一点,但我相信有一个简短的方法可以做到这一点。

谢谢。

最佳答案

extension String {
public var isBlank: Bool {
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty
}
}

@IBAction func confirmButtonWasPressed(_ sender: UIButton) {
validateLogin()
}

func validateLogin(){
guard let username = userNameTextfield.text, !username.isBlank else {
return
}
guard let password = passwordTextField.text, !password.isBlank else {
return
}

}

关于ios - 如何改进 Swift/Xcode iOS 应用程序中文本字段的控制语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com