从初始化程序返回而不初始化所有存储的属性错误Firebase

时间:2017-12-21 03:12:23

标签: swift firebase firebase-realtime-database

我正在与Firebase合作,并且正在尝试创建帖子类。我不断收到错误"从初始化程序返回而不初始化所有存储的属性"当我尝试初始化变量时。我不确定如何解决这个错误,并不完全确定问题是什么。我想知道是否有人可以提供帮助。谢谢。

import Foundation
import Firebase
import FirebaseDatabase

class Posts {
     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 _username
    }
    var userImg: 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){

        _postImg = imgUrl
        _likes = likes
        _username = username
        _userImg = userImg
        _postKey = nil
    } ///WHERE I GET THE ERROR

    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 = Database.database().reference().child("posts")
    }
}

1 个答案:

答案 0 :(得分:0)

谢谢大家的评论。在凝视了一段时间之后,我意识到在“private var _postRef:DatabaseReference”之后我没有感叹号。 “所以,我刚刚添加了”!“,结果很有效。

OLD:     private var _postRef:DatabaseReference

NEW:    private var _postRef:DatabaseReference!