Swift &引用;默认Firebase应用程序尚未配置";当它有-斯威夫特

Swift &引用;默认Firebase应用程序尚未配置";当它有-斯威夫特,swift,firebase,google-cloud-firestore,Swift,Firebase,Google Cloud Firestore,我首先让我的Firebase身份验证代码工作,一旦我添加了Firestore添加数据函数,一切都崩溃了。以下是我的错误消息: > 2018-06-22 00:31:29.238585-0400 Student Council App[4808:1902875] > [Accessibility] ****************** Loading GAX Client Bundle > **************** > 2018-06-22 00:3

我首先让我的Firebase身份验证代码工作,一旦我添加了Firestore添加数据函数,一切都崩溃了。以下是我的错误消息:

> 2018-06-22 00:31:29.238585-0400 Student Council App[4808:1902875]
> [Accessibility] ****************** Loading GAX Client Bundle
> ****************
>         2018-06-22 00:31:29.324018-0400 Student Council App[4808:1902933] 4.11.0 - [Firebase/Core][I-COR000003] The default
> Firebase app has not yet been configured. Add `[FIRApp configure];`
> (`FirebaseApp.configure()` in Swift) to your application
> initialization. Read more: --a link--.
>         2018-06-22 00:31:29.325455-0400 Student Council App[4808:1902875] *** Terminating app due to uncaught exception
> 'FIRAppNotConfiguredException', reason: 'Failed to get FirebaseApp
> instance. Please call FirebaseApp.configure() before using Firestore'
>         *** First throw call stack:
>         (0x18503ad8c 0x1841f45ec 0x104d4aefc 0x104c93328 0x104c935c4 0x18f27dedc 0x18f3df628 0x18f3df360 0x18f27db84 0x18f3df628
> 0x18f3df7a0 0x18f3df360 0x18f27ced4 0x18f589d88 0x18efebfd8
> 0x18ec09254 0x18ebd7550 0x18f207a0c 0x18ebd6e4c 0x18ebd6ce8
> 0x18ebd5b78 0x18f86b72c 0x18ebd5268 0x18f6509b8 0x18f79eae8
> 0x18ebd4c88 0x18ebd4624 0x18ebd165c 0x18ebd13ac 0x187838470
> 0x187840d6c 0x10657d220 0x106589850 0x18786c878 0x18786c51c
> 0x18786cab8 0x184fe3404 0x184fe2c2c 0x184fe079c 0x184f00da8
> 0x186ee3020 0x18eee178c 0x104c944c4 0x184991fc0)
>         libc++abi.dylib: terminating with uncaught exception of type NSException
我显然已在AppDelegate中配置了以下各项:

import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }
}
这是我的视图控制器:

    import UIKit
        import Firebase
        import FirebaseAuth
        class ViewController: UIViewController, UITextFieldDelegate {
            @IBOutlet weak var passwordTextField: UITextField!
            @IBOutlet weak var emailTextField: UITextField!
        let db = Firestore.firestore()
        let userID = Auth.auth().currentUser!.uid
        override func viewDidLoad() {
            super.viewDidLoad()
            self.passwordTextField.delegate = self
            // Do any additional setup after loading the view, typically from a nib.
        }
        override func viewDidAppear(_ animated: Bool) {
            if Auth.auth().currentUser != nil {
                self.presentLoggedInScreen()
            }
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            self.view.endEditing(true)
        }
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            passwordTextField.resignFirstResponder()
            return (true)
        }
        @IBAction func logInTapped(_ sender: Any) {
            if let email = emailTextField.text, let password = passwordTextField.text {
                Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
                    if let firebaseError = error {
                        print(firebaseError.localizedDescription)
                        return
                    }
                    self.presentLoggedInScreen()
                })
            }
        }
        @IBAction func createAccountTapped(_ sender: Any) {
            if let email = emailTextField.text, let password = passwordTextField.text {

                Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
                    if let firebaseError = error {
                        print(firebaseError.localizedDescription)
                        return
                    }
self.db.collection("users").document(self.userID).setData([ "email": email ])
                    self.presentLoggedInScreen()
                })
            }
        }
        func presentLoggedInScreen() {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let loggedInVc:LoggedInVc = storyboard.instantiateViewController(withIdentifier: "LoggedInVc") as! LoggedInVc
            self.present(loggedInVc, animated: true, completion: nil)
        }
    }
导入UIKit
进口火基
导入FirebaseAuth
类ViewController:UIViewController、UIExtFieldDelegate{
@IBOutlet弱var密码文本字段:UITextField!
@IBOutlet弱var emailTextField:UITextField!
设db=Firestore.Firestore()
让userID=Auth.Auth().currentUser!.uid
重写func viewDidLoad(){
super.viewDidLoad()
self.passwordTextField.delegate=self
//加载视图后,通常从nib执行任何其他设置。
}
覆盖函数视图显示(u动画:Bool){
如果Auth.Auth().currentUser!=nil{
self.presentLoggedInScreen()
}
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
self.view.endEditing(true)
}
func textField应该返回(textField:UITextField)->Bool{
passwordTextField.resignFirstResponder()辞职
返回(真)
}
@iAction func登录标记(\发送方:任何){
如果让email=emailTextField.text,则让password=passwordTextField.text{
Auth.Auth()
如果让firebaseError=错误{
打印(firebaseError.localizedDescription)
返回
}
self.presentLoggedInScreen()
})
}
}
@iAction func createAccountTapped(\发送方:任何){
如果让email=emailTextField.text,则让password=passwordTextField.text{
Auth.Auth().createUser(带电子邮件:电子邮件,密码:密码,完成:{user,错误在
如果让firebaseError=错误{
打印(firebaseError.localizedDescription)
返回
}
self.db.collection(“users”).document(self.userID).setData([“email”:email])
self.presentLoggedInScreen()
})
}
}
func presentLoggedInScreen()函数{
让故事板:UIStoryboard=UIStoryboard(名称:“Main”,bundle:nil)
让loggedInVc:loggedInVc=storyboard.instanceeviewController(标识符为:“loggedInVc”)作为!loggedInVc
self.present(loggedInVc,动画:true,完成:nil)
}
}

很明显,我对Swift和应用程序开发还不熟悉,这可能是非常愚蠢的事情。提前谢谢

尝试调用
let db=Firestore.Firestore()
let userID=Auth.Auth().currentUser!。uid
在viewDidLoad方法中

另一个解决方案是写:

lazy var db = Firestore.firestore()
lazy var userID = Auth.auth().currentUser!.uid

非常感谢。这太荒谬了,因为它导致了那个问题。