2017-05-08 11 views
-3
let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert) 



     let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in 
      if let field = alertController.textFields?[0] { 
       // store your data 
       UserDefaults.standard.set(try.text, forKey: "userEmail") 
       UserDefaults.standard.synchronize() 
      } else { 
       // user did not fill field 
      } 
     } 

     let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in } 
     alertController.addTextField { (textField) in 
      textField.placeholder = "Email" 
     } 


     alertController.addAction(confirmAction) 
     alertController.addAction(cancelAction) 

     self.present(alertController, animated: true, completion: nil) 
+0

これにテキストを追加します。質問のタイトルは、コード以外の唯一のテキストではありません。 – Aziuth

+0

質問は何ですか? – JeremyP

答えて

1

タイポ:

UserDefaults.standard.set(field.text, forKey: "userEmail") 

なく

UserDefaults.standard.set(try.text, forKey: "userEmail") 

エラーメッセージがそれを指摘:

... value of type '(_) throws ->()'

+0

これは私が考える正解です。 'try'はクロージャが投げているようにコンパイラを見せます。 – JeremyP

-1

使用この....

let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (action) -> Void in 
     if let field = alertController.textFields?[0] { 
      // store your data 
      UserDefaults.standard.set(try.text, forKey: "userEmail") 
      UserDefaults.standard.synchronize() 
     } else { 
      // user did not fill field 
     } 
    } 

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in 
    alertController.addTextField { (textField) in 
     textField.placeholder = "Email" 
    }) 
関連する問題