如何使用 Swift 中的数据数组递归创建模态框

How to recursively create a Modal with an array of data in Swift

我需要以正确的顺序递归添加这些注释。

目前我收到了一系列未分类的评论。 这些评论可以是顶级的,也可以是顶级的child。

我正在使用这种递归方法来添加它们,但我似乎做错了。

结果应该是 CommentModal 的数组 [CommentModal]

供参考: postID 是 child 所属的 topLevel 评论。每个 child 评论都会有一个 postID 以便他们知道他们属于哪里。如果 child 有一个 child,顶层 child 的 commentID 将是 child 的 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 { [=13=].postID == comment.commentID }
            discardableCachedComments.removeAll { [=13=].postID == comment.commentID }
            
            let commentModal = CommentModal(mainComment: comment)
            
            if children.count >= 1 {
                commentModal.childComment = addChildren(from: children)
            }
            discardableCachedComments.removeAll { [=13=].commentID == comment.commentID }
            result.append(commentModal)
        }
        
        return result
    }

期望的输出

使用上面的 data 我想看到的结果是:

3 个顶级 CommentModal 的数组。 每个顶级 CommentModals 都有一个 childComment,它是 CommentModal 的数组。对于其中一个 childComment,我们还会看到它在 childComment.

中有两个值

如果您看到数据,您将看到 postIDcommentID 是如何组装的,以便将它们正确添加到各自的模态中。

我更改了几个名称以使语义上的内容更容易理解,但这应该能说明要点。我还将 CommentModal 更改为 struct,因为这使初始化更容易,但您可以将其改回。

这应该 copy/pastable 进入游乐场:

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 { [=10=].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([=10=], indent: indent + 4)}
    }
}

let result = createCommentModels(rootKey: "RootPostID", input: data)

result.forEach {
    printModal([=10=])
}

产生:

116
 - Children:
     216
     - Children:
         316
         317
 117
 - Children:
     217
 118
 - Children:
     218