当文本字段为空时设置默认值 swift



我想知道是否有办法将自定义值设置为空的UITextField。

上下文:我必须让用户每天为整个月设置自己的时间表,如果用户在某一天没有任何内容,那么他不必在相应的日期字段中写字,它应该在他的日程安排的那一天显示一个"x"。

我正在与火力基地合作以节省天数值。

这是我的代码片段:

@objc func SaveSchedule() {
        let userID = Auth.auth().currentUser?.uid
        let ref = Database.database().reference().child("Users").child(userID!).child("Schedule")

        let values = ["dim1": self.edtDim1.text ?? "x",
                      "lun1": self.edtLun1.text!,
                      "mar1": self.edtLun1.text!,
                      "mer1": self.edtLun1.text!,
                      "jeu1": self.edtLun1.text!,
                      "ven1": self.edtLun1.text!,
                      "sam1": self.edtLun1.text!,
                      "dim2": self.edtDim1.text!,
                      "lun2": self.edtLun1.text!,
                      "mar2": self.edtLun1.text!,
                      "mer2": self.edtLun1.text!,
                      "jeu2": self.edtLun1.text!,
                      "ven2": self.edtLun1.text!,
                      "sam2": self.edtLun1.text!,
                      "dim3": self.edtDim1.text!,
                      "lun3": self.edtLun1.text!,
                      "mar3": self.edtLun1.text!,
                      "mer3": self.edtLun1.text!,
                      "jeu3": self.edtLun1.text!,
                      "ven3": self.edtLun1.text!,
                      "sam3": self.edtLun1.text!,
                      "dim4": self.edtDim1.text!,
                      "lun4": self.edtLun1.text!,
                      "mar4": self.edtLun1.text!,
                      "mer4": self.edtLun1.text!,
                      "jeu4": self.edtLun1.text!,
                      "ven4": self.edtLun1.text!,
                      "sam4": self.edtLun1.text!] as [NSString : Any]
        ref.setValue(values)
    }

如您所见,我已经尝试过"dim1"??如果为空,则放置"x"。不行。顺便说一下,我的日子是法国日子的前缀。

的问题:如果没有为文本字段设置值,我如何在我的 Firebase 中设置默认的"x"?如果可能的话,我更喜欢一个简单的"一行代码"。

提前感谢,祝你白天/晚上/晚上愉快

一个文本字段的默认值为空字符串",所以你可以试试

extension UITextfield
{
  var:currentValue:String {
      return self.text != "" ? self.text : "x"
  }
}

然后使用

nameTexf.currentValue

试试这个:

    @objc func SaveSchedule() {
    let userID = Auth.auth().currentUser?.uid
    let ref = Database.database().reference().child("Users").child(userID!).child("Schedule")
    let dimValue = self.edtDim1.text! == "" ? "x" : self.edtDim1.text!
    let lunValue = self.edtLun1.text! == "" ? "" : self.edtLun1.text!
    let values = ["dim1": dimValue,
                  "lun1": lunValue,
                  "mar1": lunValue,
                  "mer1": lunValue,
                  "jeu1": lunValue,
                  "ven1": lunValue,
                  "sam1": lunValue,
                  "dim2": dimValue,
                  "lun2": lunValue,
                  "mar2": lunValue,
                  "mer2": lunValue,
                  "jeu2": lunValue,
                  "ven2": lunValue,
                  "sam2": lunValue,
                  "dim3": dimValue,
                  "lun3": lunValue,
                  "mar3": lunValue,
                  "mer3": lunValue,
                  "jeu3": lunValue,
                  "ven3": lunValue,
                  "sam3": lunValue,
                  "dim4": dimValue,
                  "lun4": lunValue,
                  "mar4": lunValue,
                  "mer4": lunValue,
                  "jeu4": lunValue,
                  "ven4": lunValue,
                  "sam4": lunValue] as [NSString : Any]
    ref.setValue(values)
}

这将像这样填充您的值数组:

[sam2: "", mar3: "", dim4: "x", ven2: "", mer3: "", sam1: "", lun3: "", lun1: "", mer2: "", jeu3: "", lun4: "", dim1: "x", sam4: "", ven1: "", mer4: "", lun2: "", jeu4: "", dim3: "x", ven3: "", sam3: "", mar2: "", mar4: "", mer1: "", jeu1: "", jeu2: "", ven4: "", mar1: "", dim2: "x"]

您还可以设置"edtLun1"的默认值

最新更新