gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:29:55 28 4
gpt4 key购买 nike

我面临在以下模型类代码中的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com