title: swift I want to check the alert text field text is greater than 0
body: <p>I want textField text to be greater than 0 before I can confirm</p>
  
  <p>I have reference to this <a href="https://stackoverflow.com/a/41664043/9930660">example</a> </p>
  
  <p>but it's not work</p>
  
  <p>How can I change the following code better?</p>
  
  <p>Is there an easier way to check the guard let?</p>
  
  <pre><code>func addNewList() {
      let addAlert = UIAlertController(title: "新增列表", message: "請輸入列表名稱", preferredStyle: .alert)
      addAlert.addTextField { (textfied: UITextField) in
          textfied.placeholder = "列表名稱"
          textfied.addTarget(addAlert, action: #selector(addAlert.textDidChanged), for: .editingChanged)
      }
  
      let confirm = UIAlertAction(title: "確認", style: .default) { (action: UIAlertAction) in
  
          guard let name = addAlert.textFields?.first?.text else { return }
  
          let newList = List(name: name, completed: false)
          self.item?.lists.append(newList)
  
          let indexPath = IndexPath(row: self.listTableView.numberOfRows(inSection: 0), section: 0)
          self.listTableView.insertRows(at: [indexPath], with: .left)
      }
  
      let cancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  
      addAlert.addAction(cancel)
      confirm.isEnabled = false
      addAlert.addAction(confirm)
  
      self.present(addAlert, animated: true, completion: nil)
  }
  
  extension UIAlertController {
      func checkItemName(_ text: String) -&gt; Bool{
          return text.count &gt; 0
      }
  
      @objc func textDidChanged() {
          if let name = textFields?.first?.text, let action = actions.last {
             action.isEnabled = checkItemName(name)
          }
      }
  }
  </code></pre>