当textfield为空swift时设置默认值

时间:2018-04-11 17:18:51

标签: swift firebase

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

背景:我必须让用户每天为整个月设置自己的日程安排,如果用户在某一天没有任何内容,那么他​​就不必写了在相应的日期字段中,它应该在其日程表的那一天显示“x”。

我正在使用firebase来保存天数值。

以下是我的代码片段:

@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”?如果可能的话,我更喜欢简单的“单行代码”。

提前致谢, 有一个美好的一天/傍晚/晚上

2 个答案:

答案 0 :(得分:0)

文本字段的默认值为空字符串“”,因此您可以尝试

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

}

然后使用

nameTexf.currentValue

答案 1 :(得分:0)

试试这个:

    @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”

的默认值