错误" {if if statement"

时间:2016-07-27 02:11:29

标签: swift firebase

我正在尝试在我的应用中设置Firebase,并且我试图为某人的登录信息不正确添加一些条件。我添加了user, error in。出于某种原因,我似乎可以使if语句正常工作。包含以下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");
        }

    }



        }}}

1 个答案:

答案 0 :(得分: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

中详细了解它们