在下面的代码中,我遇到了self
在self.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)
}
}
答案 0 :(得分:0)
您正在进行某种循环依赖,要访问字典变量,您需要一个实例,要创建该实例,您需要调用init,但再次要调用init,则必须初始化字典变量,依此类推。您可以将字典变量设为静态。