无法在类型上使用实例成员-Firebase

时间:2018-06-20 21:41:12

标签: swift firebase

我是Swift的新手,我正在学习有关如何使用Xcode和Firebase创建社交媒体应用程序的教程。但是,我收到此错误:

  

实例成员'database'不能用于'DatabaseReference'类型

这是我的代码:

import Foundation
import Firebase

class Post {

private var _username: String!
private var _userImg: String!
private var _postImg: String!
private var _likes: Int!
private var _postKey: String!
private var _postRef: DatabaseReference

var username: String {

    return _userImg
}

var postImg: String {
    get {
        return _postImg
    }set {
        _postImg = newValue
    }
}

var likes: Int {
    return _likes
}

var postKey: String {
    return _postKey
}


init(imgUrl: String, likes: Int, username: String, userImg: String) {
    _likes = likes
    _postImg = postImg
    _username = username
    _userImg = userImg
}

init(postKey: String, postData: Dictionary<String, AnyObject>) {
    _postKey = postKey

    if let username = postData["username"] as? String {
        _username = username
    }

    if let userImg = postData["userImg"] as? String{
        _userImg = userImg
    }

    if let postImage = postData["imageUrl"] as? String {
        _postImg = postImage
    }

    if let likes = postData["likes"] as? Int {
        _likes = likes
    }

_postRef = DatabaseReference.database().reference().child("posts")
}
}

我在倒数第三行显示错误:

_postRef = DatabaseReference.database().reference().child("posts")

2 个答案:

答案 0 :(得分:0)

database属性是一个实例类型,这意味着它必须由DatabaseReference的实例引用。您对DatabaseReference.database的呼叫正在访问一个类或静态类型。您需要将呼叫更改为DatabaseReference的实例。

大概,您需要初始化DatabaseReference的实例。我不知道Firebase知道需要什么,但这将解决您的问题。

本质上:

let databaseReference = DatabaseReference() // Likely won't work, but some init method here will
_postRef = databaseReference.database()... // Whatever you need here

答案 1 :(得分:0)

听起来您正在寻找其中一个:

_postRef = Database.database().reference("posts")

_postRef = DatabaseReference.root.child("posts")