2017-07-28 14 views
0

else文を試してみると、このエラーは「未解決の識別子が使用されています」というエラーが表示されます。私はそれを修正し、まだUIAlertを持っていますか?登録時のエラー処理でエラーが発生する

 @IBAction func registerTapped(_ sender: Any) { 
     print("tapped") 
     let namec = nameTextField.text 
     if let email = emailTextField.text, let pass = passwordTextField.text, let name = (namec?.capitalized.isEmpty)! ? nil:namec?.capitalized { 
      FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in 
       if user != nil { 
        //user found 


        let interval = NSDate().timeIntervalSince1970 
        let date = Date(timeIntervalSince1970: interval) 
        let dateFormatter = DateFormatter() 
        dateFormatter.dateFormat = "dd/MM/yyyy/HH/mm/SS" 
        // you can change the date format to whatever you wants 
        let dateString = dateFormatter.string(from: date) 
        print(dateString) 
        self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": dateString]) 
        print("User Created And Added To Database", email, name, dateString) 
        self.performSegue(withIdentifier: "registertologin", sender: self) 
       } 
      }) 
     }     else { 
      print(error!) 
      let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert) 
      alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil)) 
      self.present(alert, animated: true, completion: nil) 
     } 
    } 
} 
+0

エラー変数がコードの外にありますブロックし、その範囲に存在しません、私はそのための簡単な答えを書くよ –

答えて

0

errorコードブロック内で、あなたのelse文では、error変数が存在しないではない、コードがprobbalyより次のようになります。

@IBAction func registerTapped(_ sender: Any) { 
     print("tapped") 
     let namec = nameTextField.text 
     if let email = emailTextField.text, let pass = passwordTextField.text, let name = (namec?.capitalized.isEmpty)! ? nil:namec?.capitalized { 
      FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in 
       if error != nil { 
        print(error!) 
        let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert) 
        alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil)) 
        self.present(alert, animated: true, completion: nil) 
       } else { 
        //Check if this is actually necessary, probbaly if user is nil that means your error variable is set to something, making this check unnecessary. 
        if user != nil { 
         //user found 
         let interval = NSDate().timeIntervalSince1970 
         let date = Date(timeIntervalSince1970: interval) 
         let dateFormatter = DateFormatter() 
         dateFormatter.dateFormat = "dd/MM/yyyy/HH/mm/SS" 
         // you can change the date format to whatever you wants 
         let dateString = dateFormatter.string(from: date) 
         print(dateString) 
         self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": dateString]) 
         print("User Created And Added To Database", email, name, dateString) 
         self.performSegue(withIdentifier: "registertologin", sender: self) 
        } else { 
         self.showAlertView(withTitle: "Error", andMessage: "User not found") 
        } 
       } 
      }) 
     } else { 
      self.showAlertView(withTitle: "Missing input", andMessage: "Please make sure email and password fields are filled.") 
     } 
    } 
} 


func showAlertView(withTitle aTitle: String, andMessage aMessage: String) { 
    let alertController = UIAlertController(title: aTitle message: aMessage, preferredStyle: .alert) 
    let actionOk = UIAlertAction(title: "OK", style: .default, handler: nil) 
    alertController.addAction(actionOk) 

    //This part of code needs to run on the main thread since your alert might be 
    //executed form the registration code block, which might be on a background thread 
    DispatchQueue.main.async { 
     self.presentViewController(alertController, animated: true, completion: nil) 
    } 
} 
+0

私はUIAlertをやってどうやって行くだろうか? –

+0

その回答を更新することができます –

+0

お願いします。 –

関連する問題