数据库参考错误

时间:2018-02-24 22:38:34

标签: swift firebase firebase-realtime-database

在我的iOS项目中,我使用Firebase创建实时数据库。我已经在AppDelegate.swift中imported it了。

imported it

尝试在应用中引用我的数据库时,会出现error

error

有关如何解决此问题的任何想法?这是代码:

import UIKit 

import Firebase


class TableViewController: UITableViewController  {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        post()

        }

    func post(){


        let title = "Title"
        let message = "Message"

        let post : [String : AnyObject] = ["title" : title as AnyObject,
                                           "message" : message as AnyObject]

        let databaseRef = FirebaseApp.database().reference()

        databaseRef.child("Posts").childByAutoId().setValue(post)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.
    }


}

3 个答案:

答案 0 :(得分:1)

您需要先配置firebase,例如:

FirebaseApp.configure()

然后你可以参考数据库:

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

FirebaseApp不包含名为database()

的方法

答案 1 :(得分:1)

如果您的Firebase /数据库版本为4.9.0,则以下内容应为您提供良好的开端。

import UIKit
import Firebase

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

    override init() {
        super.init()
        FirebaseApp.configure()
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        return true
    }
}

import UIKit
import FirebaseDatabase

class TableViewController: UITableViewController {
    // MARK: - Instance variables
    var databaseRef: DatabaseReference!

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        //getting a reference to the node artists
        databaseRef = Database.database().reference()
    }
}

答案 2 :(得分:0)

将“Firebase/Database”添加到您的 Pod:

pod 'Firebase/Core'
pod 'Firebase/Database'

然后更新 pods

pod install

确保在您的代码中导入 firebase

import Firebase

并将其用作

 FirebaseApp.configure()
 var ref: DatabaseReference!
 ref = Database.database().reference()
 ref.setValue("Testing")

关于ERD diagrams sub-type relations的更多信息