即使值为 nil,Guard Let 语句也不会触发
Guard Let statement not triggering even when values are nil
这是我的 UIBarButtonItem:
@IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
print("doneButton Pressed")
// Guard statement ensures that all fields have been satisfied, so that the JumpSpotAnnotation can be made, and no values are nil
guard let name = nameTextField.text,
let estimatedHeight = estimatedHeightTextField.text,
let locationDescription = descriptionTextView.text else {
nameRequiredLabel.isHidden = false
heightRequiredLabel.isHidden = false
descriptionRequiredLabel.isHidden = false
print("User did not put in all the required information.")
return
}
IBAction 中其下方的代码无关紧要,因为这是一个 guard let 问题。即使我真的将值设置为 nil,它也不会触发。在我的 viewDidLoad 中,我输入:
nameTextField.text = nil
estimatedHeightTextField.text = nil
descriptionTextView.text = nil
当我按下按钮时,在不更改这些文本的值的情况下,guard let 语句仍然不会触发,下面的函数的其余部分将执行。任何想法为什么?谢谢
如果您只是检查文本字段是否为空,那么您可以这样做:
guard
let estimatedHeight = estimatedHeightTextField.text,
!estimatedHeight.isEmpty,
let locationDescription = descriptionTextView.text,
!locationDescription.isEmpty
else {
nameRequiredLabel.isHidden = false
heightRequiredLabel.isHidden = false
descriptionRequiredLabel.isHidden = false
print("User did not put in all the required information.")
return
}
检查这个答案:
UITextField
文本 属性 默认值为 emptyString
。它永远不会 return nil
即使你在检查它的值之前给它赋值 nil 。顺便说一句,UIKeyInput
协议有一个名为 hasText
的 属性 正是为了这个目的。如果您还想避免用户只输入空格和新行,您可以 trim 在检查它是否为空之前输入它们。您可以扩展 UITextInput
并实现您自己的 isEmpty
属性。这将涵盖 UITextField
和 UITextView
的单一实现:
extension UITextInput {
var isEmpty: Bool {
guard let textRange = self.textRange(from: beginningOfDocument, to: endOfDocument) else { return true }
return text(in: textRange)?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == true
}
}
let textField = UITextField()
textField.text = " \n "
textField.isEmpty // true
let textView = UITextView()
textView.text = " \n a"
textView.isEmpty // true
这是我的 UIBarButtonItem:
@IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
print("doneButton Pressed")
// Guard statement ensures that all fields have been satisfied, so that the JumpSpotAnnotation can be made, and no values are nil
guard let name = nameTextField.text,
let estimatedHeight = estimatedHeightTextField.text,
let locationDescription = descriptionTextView.text else {
nameRequiredLabel.isHidden = false
heightRequiredLabel.isHidden = false
descriptionRequiredLabel.isHidden = false
print("User did not put in all the required information.")
return
}
IBAction 中其下方的代码无关紧要,因为这是一个 guard let 问题。即使我真的将值设置为 nil,它也不会触发。在我的 viewDidLoad 中,我输入:
nameTextField.text = nil
estimatedHeightTextField.text = nil
descriptionTextView.text = nil
当我按下按钮时,在不更改这些文本的值的情况下,guard let 语句仍然不会触发,下面的函数的其余部分将执行。任何想法为什么?谢谢
如果您只是检查文本字段是否为空,那么您可以这样做:
guard
let estimatedHeight = estimatedHeightTextField.text,
!estimatedHeight.isEmpty,
let locationDescription = descriptionTextView.text,
!locationDescription.isEmpty
else {
nameRequiredLabel.isHidden = false
heightRequiredLabel.isHidden = false
descriptionRequiredLabel.isHidden = false
print("User did not put in all the required information.")
return
}
检查这个答案:
UITextField
文本 属性 默认值为 emptyString
。它永远不会 return nil
即使你在检查它的值之前给它赋值 nil 。顺便说一句,UIKeyInput
协议有一个名为 hasText
的 属性 正是为了这个目的。如果您还想避免用户只输入空格和新行,您可以 trim 在检查它是否为空之前输入它们。您可以扩展 UITextInput
并实现您自己的 isEmpty
属性。这将涵盖 UITextField
和 UITextView
的单一实现:
extension UITextInput {
var isEmpty: Bool {
guard let textRange = self.textRange(from: beginningOfDocument, to: endOfDocument) else { return true }
return text(in: textRange)?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == true
}
}
let textField = UITextField()
textField.text = " \n "
textField.isEmpty // true
let textView = UITextView()
textView.text = " \n a"
textView.isEmpty // true