首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数据库引用错误

数据库引用错误
EN

Stack Overflow用户
提问于 2018-02-25 06:38:34
回答 3查看 435关注 0票数 0

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

当尝试在应用程序中引用我的数据库时,会出现一个error

有什么办法解决这个问题吗?代码如下:

代码语言:javascript
复制
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.
    }


}
EN

回答 3

Stack Overflow用户

发布于 2018-02-25 06:47:54

您需要先配置firebase,示例:

代码语言:javascript
复制
FirebaseApp.configure()

然后,您可以引用数据库:

代码语言:javascript
复制
let databaseRef = Database.database().reference()

FirebaseApp不包含名为database()的方法

票数 1
EN

Stack Overflow用户

发布于 2018-02-25 10:18:11

如果你的Firebase/Database版本是4.9.0,下面的代码会给你一个好的开始。

代码语言:javascript
复制
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()
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-01-30 20:04:04

将'Firebase/Database‘添加到您的pods中:

代码语言:javascript
复制
pod 'Firebase/Core'
pod 'Firebase/Database'

然后更新pods

代码语言:javascript
复制
pod install

确保将firebase导入到您的代码中

代码语言:javascript
复制
import Firebase

并将其用作

代码语言:javascript
复制
 FirebaseApp.configure()
 var ref: DatabaseReference!
 ref = Database.database().reference()
 ref.setValue("Testing")

有关Firebase help的更多信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48968355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档