ios - 如何纠正在 "self' 调用或赋值给 'self.init' 之前使用 'self' 的错误?

标签 ios swift firebase model google-cloud-firestore

我面临在以下模型类代码中的 self.init 调用或分配给 self 之前使用 self 的错误tableview单元格项目,它发生在我尝试获取表格单元格项目的文档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)
    }
}

最佳答案

你有一些循环依赖,要访问字典变量,你需要一个实例,要创建实例,你需要调用 init,但再次调用 init,你应该初始化字典变量,依此类推。您可以将字典变量设置为静态。

关于ios - 如何纠正在 "self' 调用或赋值给 'self.init' 之前使用 'self' 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719368/

相关文章:

ios - 如何在 UITableView 的 switch 语句中创建变量?

iphone - 数据源和 TableView 的问题

ios - LLVM 错误 : Broken function found, 编译中止

iphone - 如何同时执行两个 UI 更新?

swift - 对象作为错误 - CoreData 关系

javascript - 无法在简单的 HTML、JS 页面上使用 firebase 身份验证进行身份验证

android - 应用未打开时显示抬头通知

arrays - SwiftUI 复选标记单行

ios - 当我缩放 GMaps 自定义标记 View 时更改位置

ios - 错误: firebase code = 9999 Too many requests