【问题标题】:Error "{ expected after if statement"错误“{ 预期在 if 语句之后”
【发布时间】:2016-07-27 02:11:29
【问题描述】:

我正在尝试在我的应用程序中设置 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");
        }

    }



        }}}

【问题讨论】:

    标签: swift firebase


    【解决方案1】:

    错误在这里:

     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 中详细了解它们。

    【讨论】:

    • 如果这个答案是正确的,那就是对编译器编写者技能的控诉。空格应该只用于消除歧义,这里没有歧义。
    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多