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

标签 arrays swift database struct

我正在尝试优化我的代码以最小化不必要信息的下载大小。
我的目标是链接 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/

相关文章:

arrays - MongoDB 数组 - 原子更新或推送元素

c++ - 与多态性一起使用的数组的奇怪输出

ios - 创建变量并在 View Controller 中的各种函数中使用

swift - Swift 中的尾随闭包语法是什么?

swift - 标签中的文本从屏幕溢出 iOS Swift

php - 在数据库中查找现有的电子邮件和用户名

mysql - 用于存储内容主题标签/关键字的最佳 mysql 字段类型

ruby - 从 Ruby 中的哈希数组中删除哈希

c++ - 3d -> 1D 数组索引

.net - 存储 PBKDF2 加密密码时使用什么数据类型?