如何纠正在调用“ self.init”或分配给“ self”之前使用的“ self”错误?

时间:2019-08-30 01:20:28

标签: ios swift firebase model google-cloud-firestore

在下面的代码中,我遇到了selfself.init调用或向self赋值之前在表视图单元格项的模型类中使用的错误,它是在我尝试获取表格单元格项目的文档ID。

该怎么办?请推荐。

import Foundation
import Firebase
import FirebaseFirestore


protocol DocumentSerializable  {
    init?(dictionary:[String:Any])
}


struct Post {
    var _postKey:  String!

     var _username: String!
     var _postTitle: String!
     var _postcategory:  String!
     var _postContent:  String!




    var dictionary:[String:Any] {
        return [
            "username": _username,
            "postTitle":_postTitle,
            "postcategory":_postcategory,
            "postContent":_postContent,
            "postKey":_postKey
        ]
    }

}

extension Post : DocumentSerializable {



    init?(dictionary: [String : Any]) {

        guard let postKey = key,
            let username = dictionary["username"] as? String,

            let postTitle = dictionary["postTitle"] as? String,
            let postcategory = dictionary["postcategory"] as? String,
            let postContent = dictionary["postContent"] as? String   else { return nil }


        self.init(_postKey: postKey, _username: username ,_postTitle: postTitle, _postcategory: postcategory, _postContent: postContent)
    }
}

1 个答案:

答案 0 :(得分:0)

您正在进行某种循环依赖,要访问字典变量,您需要一个实例,要创建该实例,您需要调用init,但再次要调用init,则必须初始化字典变量,依此类推。您可以将字典变量设为静态。