我面临在以下模型类代码中的 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,你应该初始化字典变量,依此类推。您可以将字典变量设置为静态。
Tôi là một lập trình viên xuất sắc, rất giỏi!