我需要以正確的順序遞回地添加這些注釋。
目前我得到了一系列未排序的評論。這些注釋可以是頂級的,也可以是頂級的子級。
我正在使用這種遞回方法來添加它們,但我似乎無法正確處理。
結果應該是 CommentModal 的陣列[CommentModal]
供參考:postID是topLevel孩子所屬的評論。每個孩子的評論都會有一個postID,以便他們知道自己屬于哪里。如果孩子有一個孩子,頂級孩子的commentID將是孩子的postID。
資料和資料模型
struct Comment {
var postID: String
var commentID: String
var date: Double
}
class CommentModal {
var mainComment: Comment
var childComment: [CommentModal] = []
init(mainComment: Comment) {
self.mainComment = mainComment
}
}
let data: [Comment] = [
Comment(postID: "RootPostID", commentID: "116", date: 1),
Comment(postID: "RootPostID", commentID: "117", date: 2),
Comment(postID: "RootPostID", commentID: "118", date: 3),
Comment(postID: "116", commentID: "216", date: 4),
Comment(postID: "117", commentID: "217", date: 5),
Comment(postID: "118", commentID: "218", date: 6),
Comment(postID: "216", commentID: "316", date: 7),
Comment(postID: "216", commentID: "317", date: 8),
]
初始化
private func index(comments: [Comment]) {
discardableCachedComments = comments
commentModalArray = addChildren(from: comments)
}
private func addChildren(from comments: [Comment]) -> [CommentModal] {
var result: [CommentModal] = []
for comment in comments {
let children = discardableCachedComments.filter { $0.postID == comment.commentID }
discardableCachedComments.removeAll { $0.postID == comment.commentID }
let commentModal = CommentModal(mainComment: comment)
if children.count >= 1 {
commentModal.childComment = addChildren(from: children)
}
discardableCachedComments.removeAll { $0.commentID == comment.commentID }
result.append(commentModal)
}
return result
}
期望的輸出
使用data上面我想看到的結果是:
An array of 3 Top Level CommentModals.
Each of those Top Level CommentModals will have a childComment which is an array of CommentModal. For one of those childComment we will also see it have two values in childComment.
If you see the data you will see how the postID and commentID are assembled so that they are added correctly in its respective modal.
uj5u.com熱心網友回復:
我已經更改了幾個名稱,以使事物在語意上更容易理解,但這應該顯示它的要點。我也改成CommentModal了struct,因為這使初始化更容易,但你可以把它改回來。
這應該可以復制/粘貼到 Playground 中:
struct Comment : Codable {
var parentID: String
var id: String
var date: Double
}
struct CommentModel : Codable {
var comment: Comment
var children: [CommentModel] = []
}
let data: [Comment] = [
Comment(parentID: "RootPostID", id: "116", date: 1),
Comment(parentID: "RootPostID", id: "117", date: 2),
Comment(parentID: "RootPostID", id: "118", date: 3),
Comment(parentID: "116", id: "216", date: 4),
Comment(parentID: "117", id: "217", date: 5),
Comment(parentID: "118", id: "218", date: 6),
Comment(parentID: "216", id: "316", date: 7),
Comment(parentID: "216", id: "317", date: 8),
]
func createCommentModels(rootKey: String, input: [Comment]) -> [CommentModel] {
return input
.filter { $0.parentID == rootKey }
.map { comment in
return CommentModel(comment: comment,
children: createCommentModels(rootKey: comment.id,
input: input
))
}
}
func printModal(_ input: CommentModel, indent: Int = 0) {
let indentChars = Array(repeating: " ", count: indent).joined(separator: "")
print("\(indentChars)", input.comment.id)
if !input.children.isEmpty {
print("\(indentChars) - Children:")
input.children.forEach { printModal($0, indent: indent 4)}
}
}
let result = createCommentModels(rootKey: "RootPostID", input: data)
result.forEach {
printModal($0)
}
產生:
116
- Children:
216
- Children:
316
317
117
- Children:
217
118
- Children:
218
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448952.html
標籤:arrays swift recursion recursive-datastructures
下一篇:用數字填充陣列的對角線元素
