我是 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
}
也许这是一个愚蠢的问题,但我被卡住了。
最佳答案
我只使用下面的代码,因为您不需要调用 Firebase.Database
:
let reference = Database.database().reference()
在您的 AppDelegate 文件中,您不希望做任何您想做的事情。您需要在 Controller 类中执行此操作。例如,XCode 为您创建的默认 ViewController 类。有任何问题,请告诉我。我们都去过那里!
关于ios - 当我尝试引用 firebase 数据库时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48372392/