Check string for nil & empty

后端 未结 23 1366
感动是毒
感动是毒 2020-12-07 10:53

Is there a way to check strings for nil and \"\" in Swift? In Rails, I can use blank() to check.

I currently have this, but i

23条回答
  •  悲&欢浪女
    2020-12-07 11:14

    helpful when getting value from UITextField and checking for nil & empty string

    @IBOutlet weak var myTextField: UITextField!
    

    Heres your function (when you tap on a button) that gets string from UITextField and does some other stuff

    @IBAction func getStringFrom_myTextField(_ sender: Any) {
    
    guard let string = myTextField.text, !(myTextField.text?.isEmpty)!  else { return }
        //use "string" to do your stuff.
    }
    

    This will take care of nil value as well as empty string.

    It worked perfectly well for me.

提交回复
热议问题