2017-05-08 2 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" 
    }) 
관련 문제