【发布时间】:2021-12-30 10:36:40
【问题描述】:
长话短说,我必须实现一个 comments 具有嵌套 cmets(1 级)的视图控制器,遵循以下结构:
-- Comment
-- reply to comment
-- reply to comment
-- Comment
-- Comment
我曾询问后端是否可以在内部提供一个可选的子 cmets (let children[Comment]?) 数组,因此,当我在集合视图单元格中删除父级时,我不必等待后端重新加载数据,从当前数据源中删除对象。
相反,后端提出了这个 json,如果 id 和 root_comment_id 相同,您可以在其中了解哪个是父级。如果不是,则所有后续的 cmets 都属于第一个对象。 ????
{
"result": [
{
"id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"post_id": "03486c50-6a4a-48a3-a68e-374cf42686d8",
"poster": {
"id": "5b52c4ed-bd21-49fe-9439-8722e4223d50",
"username": "foobar",
"fullname": "foo bar",
"avatar_url": null
},
"body": "Nice Post",
"root_comment_id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"to_poster": null,
"created_at": "2021-11-05T14:38:15.000Z"
},
{
"id": "43fb2e48-2aae-4b01-bafa-456b927444d5",
"post_id": "03486c50-6a4a-48a3-a68e-374cf42686d8",
"poster": {
"id": "5b52c4ed-bd21-49fe-9439-8722e4223d50",
"username": "foobar",
"fullname": "foo bar",
"avatar_url": null
},
"body": "Yes I like it too",
"root_comment_id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"to_poster": null,
"created_at": "2021-11-05T14:38:46.000Z"
},
{
"id": "11a4c5d6-9db8-47c1-a472-947d8d1ac81a",
"post_id": "03486c50-6a4a-48a3-a68e-374cf42686d8",
"poster": {
"id": "5b52c4ed-bd21-49fe-9439-8722e4223d50",
"username": "foobar",
"fullname": "foo bar",
"avatar_url": null
},
"body": "Awesome!",
"root_comment_id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"to_poster": {
"id": "788343e4-3695-44e5-bda4-9b0593b7a496",
"username": "matthewzorpas",
"fullname": "Matthew Zorpas",
"avatar_url": null
},
"created_at": "2021-11-05T14:39:45.000Z"
}
],
"statusCode": 200
}
这对我来说真的没有意义,但由于他们不会改变它,我必须映射这个 json 并制作我自己的模型,看起来像这样:
struct CommentResult: Codable {
let result: [Comment]
let statusCode: Int
}
// MARK: - Result
struct Comment: Codable {
let commentID, postID: String
let poster: Poster
let body: String?
let rootCommentID: String
let toPoster: Poster?
let createdAt: Date
let children:[Comment]? // I would like to add this and append children with a root_id == to the parent id
enum CodingKeys: String, CodingKey {
case commentID = "id"
case postID = "post_id"
case poster
case body
case rootCommentID = "root_comment_id"
case toPoster = "to_poster"
case createdAt = "created_at"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.commentID = try container.decode(String.self, forKey: .commentID)
self.postID = try container.decode(String.self, forKey: .postID)
self.poster = try container.decode(Poster.self, forKey: .poster)
self.toPoster = try container.decodeIfPresent(Poster.self, forKey: .toPoster)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
let dateString = try container.decode(String.self, forKey: .createdAt)
self.createdAt = dateFormatter.date(from: dateString) ?? Date()
self.body = try container.decodeIfPresent(String.self, forKey: .body)
self.rootCommentID = try container.decode(String.self, forKey: .rootCommentID)
}
}
我知道我可能必须使用filter,但我一直在努力寻找正确的方法,而不会增加时间复杂度。
所以 json 看起来应该是这样的:
{
"result": [
{
"id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"post_id": "03486c50-6a4a-48a3-a68e-374cf42686d8",
"poster": {
"id": "5b52c4ed-bd21-49fe-9439-8722e4223d50",
"username": "foobar",
"fullname": "foo bar",
"avatar_url": null
},
"body": "Nice Post",
"root_comment_id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"to_poster": null,
"created_at": "2021-11-05T14:38:15.000Z",
"children": [ {
"id": "43fb2e48-2aae-4b01-bafa-456b927444d5",
"post_id": "03486c50-6a4a-48a3-a68e-374cf42686d8",
"poster": {
"id": "5b52c4ed-bd21-49fe-9439-8722e4223d50",
"username": "foobar",
"fullname": "foo bar",
"avatar_url": null
},
"body": "Yes I like it too",
"root_comment_id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"to_poster": null,
"created_at": "2021-11-05T14:38:46.000Z"
},
{
"id": "11a4c5d6-9db8-47c1-a472-947d8d1ac81a",
"post_id": "03486c50-6a4a-48a3-a68e-374cf42686d8",
"poster": {
"id": "5b52c4ed-bd21-49fe-9439-8722e4223d50",
"username": "foobar",
"fullname": "foo bar",
"avatar_url": null
},
"body": "Awesome!",
"root_comment_id": "bedcab34-f6b7-44a9-ab81-6d443ada580e",
"to_poster": {
"id": "788343e4-3695-44e5-bda4-9b0593b7a496",
"username": "matthewzorpas",
"fullname": "Matthew Zorpas",
"avatar_url": null
},
"created_at": "2021-11-05T14:39:45.000Z"
}
]
}
],
"statusCode": 200
}
【问题讨论】:
-
回复的深度可以达到多少?回复也能有回复吗?
-
你好@Rob ....幸运的是只有一层;)