【发布时间】:2019-08-30 01:20:28
【问题描述】:
我面临self 在self.init 调用或分配给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)
}
}
【问题讨论】:
-
我想你的意思是
guard let postKey = dictionary["postKey"] as? String。 -
但是我还需要获取documentID,会怎样?
-
documentID是postKey,不是吗? -
不,那不正确,所以我编辑了它
-
key来自哪里?你为什么使用 _ 前缀?为什么你的 struct 属性隐式地解包了可选项?
标签: ios swift firebase model google-cloud-firestore