gpt4 book ai didi

arrays - 如何链接结构?需要优化性能

转载 作者:行者123 更新时间:2023-12-02 09:47:08 26 4
gpt4 key购买 nike

我正在尝试优化我的代码以最小化不必要信息的下载大小。
我的目标是链接 testData 数组中的项目以调用另一个结构的方法。
这是一个例子:

struct Recipe: Identifiable, Hashable, Codable {
@DocumentID var id: String? = UUID().uuidString
var title: String
var description: String
var imageName: String
}
let testData = [
Recipe(title: "Recipe1", description: "Description1", imageName: "Image1"
Recipe(title: "Recipe2", description: "Description2", imageName: "Image2"
Recipe(title: "Recipe3", description: "Description3", imageName: "Image3"
]

// Then another struct, which needs to be linked to members of the array above
// Code below is incorrect, will not work
struct CommentSection {
var recipe: Recipe // would I link a variable to struct here or how else to proceed?
var user: String
var body: String
}
let commentData [
CommentSection(user: "User1", body: "body1"
CommentSection(user: "User1", body: "body1"
]
我想做的是能够将 CommentSection 中的项目与 testData 数组中的项目相关联,而无需创建子结构。
我知道类可以继承,尽管有很多应用程序绑定(bind)在结构 IE 数据库、数组等中,因此希望将其保留为结构。
我将如何继续调用 CommentSection.user & body 应该链接到 testData 的 [#] 而不通过 Recipe 结构访问它?

最佳答案

我不确定你想做什么。
但是,首先:
评论应该链接到被评论的对象 -
它应该是这样的:

struct CommentedObject {
...
}

struct Comment {
...
let ownerId, commentedObjectId: String
...
}
在哪里:
所有者 ID - 评论员 ID,
已评论对象 ID - 被评论对象的 id,
第二:

@DocumentID var id: String? = UUID().uuidString


应该是这样的 -
@DocumentID 变量 id:字符串? –
Firebase 会在对象初始化时自动创建一个 ID,
不要忘记 编码键 枚举。

关于arrays - 如何链接结构?需要优化性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64159858/

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