title: Validate text fields swift 3
body: <p>I am trying to make all of my text fields required to make sure that users does not leave it empty. I tried this but for some reason it's not working. I feel like my logic is correct, but it's an issue with swift's logic</p>
  
  <p>Here is my code:</p>
  
  <pre><code>let title = txtTitle.text!
  let location = txtWhere.text!
  let when = txtWhen.text!
  
  
      if title != nil &amp;&amp; location != nil &amp;&amp; when != nil {
  
          var titleArray = [String]()
          var whereArray = [String]()
          var whenArray = [String]()
  
          let titleObject = UserDefaults.standard.object(forKey: "saveTitle")
          let whereObject = UserDefaults.standard.object(forKey: "saveLocation")
          let whenObject = UserDefaults.standard.object(forKey: "saveWhen")
  
  
  
  
          if let tempTitle = titleObject as? [String] {
  
              titleArray = tempTitle
              titleArray.append(txtTitle.text!)
              print(titleArray)
  
          }
          else {
  
              titleArray = [txtTitle.text!]
  
          }
  
          if let tempWhere = whereObject as? [String] {
  
              whereArray = tempWhere
              whereArray.append(txtWhere.text!)
              //print(titleArray)
  
          }
          else {
  
              whereArray = [txtWhere.text!]
  
          }
  
          if let tempWhen = whenObject as? [String] {
  
              whenArray = tempWhen
              whenArray.append(txtWhen.text!)
              //print(titleArray)
  
          }
          else {
  
              whenArray = [txtWhen.text!]
  
          }
  
  
          UserDefaults.standard.set(titleArray, forKey: "saveTitle")
          UserDefaults.standard.set(whereArray, forKey: "saveLocation")
          UserDefaults.standard.set(whenArray, forKey: "saveWhen")
          txtTitle.text = ""
          txtWhere.text = ""
          txtWhen.text = ""
          txtTime.text = ""
  
      }
          else
      {
  
          errMsg.isHidden = false
          errMsg.text = "All fields are required"
  
      }
  }
  </code></pre>