2016-07-27 14 views
-2

私のアプリでFirebaseをセットアップしようとしていますが、誰かのログイン情報が正しくない場合にいくつかの条件を追加しようとしています。私はuser, error inを追加しました。何らかの理由で、if文が正しく動作するように見えるかもしれません。エラー "{i​​f after ifステートメント"

FIRAuth.auth()?.createUserWithEmail(userEmailTextField.text!, password: userPasswordTextField.text!, completion: { 

      user, error in 

      if error !=nil{ //here 

      } 
      else { 
       print("User Created") 
       self.login() 
      } 
     }) 

がそうのよう!=の後にスペースを与えます:

FIRAuth.auth()?.createUserWithEmail(userEmailTextField.text!, password: userPasswordTextField.text!, completion: { (user, error) in 

      if error != nil { //Fixes the issue 

      } 
      else { 
       print("User Created") 
       self.login() 
      } 
     }) 

それとしてここにある以下のif error!=nil{を持つ任意の行は、エラーがここにあるexpected "{" after if declaration

// 
// RegistrationViewController.swift 
// StudyBuddy 
// 
// Created by Basel Anani on 7/25/16. 
// Copyright © 2016 StudyBuddy. All rights reserved. 
// 

import UIKit 
import Firebase 

class RegistrationViewController: UIViewController { 

    @IBOutlet weak var userEmailTextField: UITextField! 
    @IBOutlet weak var userPasswordTextField: UITextField! 
    @IBOutlet weak var userConfirmPasswordTextField: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func registerButtonTapped(sender: AnyObject) { 

     let userEmail = userEmailTextField.text; 
     let userPassword = userPasswordTextField.text; 
     let userConfirmPassword = userConfirmPasswordTextField.text; 

     //Check for Empty Fields 
     FIRAuth.auth()?.createUserWithEmail(userEmailTextField.text!, password: userPasswordTextField.text!, completion: { 

      user, error in 

      if error !=nil{ 

      } 
      else { 
       print("User Created") 
       self.login() 
      } 
     }) 

     func login(){ 
       FIRAuth.auth()?.signInWithEmail(userEmailTextField.text!, password: userPasswordTextField.text!, completion: { 
       user, error in 

       if error !=nil { 

        print("Incorrect") 
       } 
       else{ 

        print("Login Successful") 
       } 




     }) 



     //Save Stored Data 

    func displayMyAlertMessage(userMessage:String) { 

     var myAlert = UIAlertController(title: "Alert", message:  "userMessage", preferredStyle: UIAlertControllerStyle.Alert); 

     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil); 

     myAlert.addAction(okAction); 

     self.presentViewController(myAlert, animated: true, completion: nil); 

     if (userEmail!.isEmpty) { 

      displayMyAlertMessage("All fields are required"); 


      return; 
     } 

     if (userPassword!.isEmpty) { 

      displayMyAlertMessage("All fields are required"); 

      return; 
     } 

     if (userConfirmPassword!.isEmpty) { 

      displayMyAlertMessage("All fields are required"); 

      return; 
     } 

     if userPassword != userConfirmPassword { 
      displayMyAlertMessage("Passwords do not match"); 
     } 

    } 



     }}} 

答えて

3

エラーを思い付きますよく:

func login(){ 
       FIRAuth.auth()?.signInWithEmail(userEmailTextField.text!, password: userPasswordTextField.text!, completion: { 
       user, error in 

       if error !=nil { //here. Change it the same way as shown above 

        print("Incorrect") 
       } 
       else{ 

        print("Login Successful") 
       } 




     }) 

そして、あなたが書いたものをさらに補完ブロックが、私はあなたがこのように書くことをお勧めしたい:彼らは最後の引数がそうであるように

FIRAuth.auth()?.createUserWithEmail(userEmailTextField.text!, password: userPasswordTextField.text!) { (user, error) in 
//your code 
} 

FIRAuth.auth()?.signInWithEmail(userEmailTextField.text!, password: userPasswordTextField.text!) { (user, error) in 
//your code 
} 

彼らはtrailing closuresと呼ばれています関数。あなたはdocsでそれらについての良い読書をすることができます。

+0

この回答が正しい場合は、コンパイラライターのスキルの告発です。空白はあいまいさを取り除くために必要なだけでなく、あいまいさもありません。 – EJP