当我尝试引用firebase数据库时,应用程序崩溃

时间:2018-01-21 22:31:08

标签: ios swift firebase

我是Swift和Firebase的新手。我目前的项目是在Xcode 8中。我接下来的几个教程,现在我被卡住了。我可能已经发现了这个问题。当我尝试引用数据库时,应用程序崩溃了。这发生在以下行:

var ref = Firebase.Database().reference()

我尝试过很多东西,比如添加以下几行代码:

var ref: DatabaseReference!
ref = Database.database().reference()

但那并没有奏效。我还试图删除一些文件并制作一个新的podfile并使用终端做同样的事情。在我的Podfile中,我有以下几点:

pod 'Firebase/Database'
pod 'Firebase/Auth'
pod 'Firebase/Storage'
pod 'Firebase/Core'

这是我目前的代码:

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        var ref = Firebase.Database().reference()

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = UINavigationController(rootViewController: ViewController())

        return true
    }

也许这是一个愚蠢的问题但是我被卡住了。

1 个答案:

答案 0 :(得分:1)

我只是使用下面的代码,因为您不需要致电Firebase.Database

let reference = Database.database().reference()

您希望在AppDelegate文件中不要做任何类似于您尝试做的事情。您希望在控制器类中执行此操作。例如,XCode为您创建的默认ViewController类。有任何问题,请告诉我。我们都去过那儿!