2016-07-27 52 views
-2

我試圖在我的應用程序中設置Firebase,並且正在嘗試爲某人的登錄信息不正確添加一些條件。我加了user, error in。出於某種原因,我似乎可以讓if語句正常工作。具有以下if error!=nil{任何系標配了錯誤expected "{" after if declaration錯誤「{預計在if語句後」

// 
// 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

的錯誤是在這裏:

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() 
      } 
     }) 

正是在這裏爲好:

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