私はUIAlertControllerを実行しています。このメッセージを受け取りました: "オプションのチェックで使用されるタイプuitextfieldの非オプション式"。 これは私が「もし私のフィールドをつかまえたら」という行にあります。オプションのチェックで使用されるタイプuitextfieldの非オプション式
私はこのコードを持って:
let alertController = UIAlertController(title: "Adding", message: "Type something", preferredStyle: UIAlertControllerStyle.alert)
let DestructiveAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
if let field : UITextField = (alertController.textFields? [0])! as UITextField {
if field.text != "" {
//my actions
}
}
let okAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
}
alertController.addTextField { (textField) in
textField.placeholder = "Type designation here"
}
alertController.addAction(okAction)
alertController.addAction(DestructiveAction)
self.present(alertController, animated: true, completion: nil)
}
誰がどのようにこの警告をしてください削除する教えてもらえますか? "!"を削除しようとしました。成功なし。
テキストフィールドが存在するかどうかは、オプションではないため、チェックしないでください。これを使うことができます: 'let field = alertController.textFields.first!'そしてあなたの行動を実行できるよりも –