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

Vla*_*lav 6 database arrays struct swift

我正在尝试优化我的代码以最小化不必要信息的下载大小。

我的目标是将 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"
]
Run Code Online (Sandbox Code Playgroud)

我想要做的是能够将 CommentSection 中的项目与 testData 数组中的项目相关联,而无需创建子结构。

我知道类可以继承,虽然有很多应用程序绑定在结构 IE 数据库、数组等中,因此希望将其保留为结构。

如果不通过 Recipe 结构访问它,我将如何继续调用应链接到 testData 的 [#] 的 CommentSection.user & body?

Dim*_*ovo 0

我不确定你想做什么。\n但是,首先:\na 注释应该链接到正在注释的对象 -\nit 应该如下所示:

\n
struct CommentedObject {\n    ...\n}\n\nstruct Comment {\n    ...\n    let ownerId, commentedObjectId: String\n    ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

其中:\ nownerId - 评论者 ID,\n commentedObjectId - 评论对象的 ID,

\n

第二:

\n
\n

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

\n
\n

应该看起来像这样 -\n@DocumentID var id: String? \xe2\x80\x93\n当对象初始化时,Firebase 会自动创建一个 ID,\n不要忘记CodingKeys枚举。

\n