如何使用 Swift 在 iOS 中为自身的数组动态创建数据模型



如何为下面给出的示例创建一个数据模型,以便我可以动态开发任何大小的数组?我的数据会像

let dataArray = [[
            self.keyIndent: 0, self.keyTitle: "title 0", self.keyChildren: [[
                self.keyIndent: 1, self.keyTitle: "title 01", self.keyChildren: [[
                    self.keyIndent: 2, self.keyTitle: "title 011", self.keyChildren: [[
                        self.keyIndent: 3, self.keyTitle: "title 0111", self.keyChildren: [[
                            self.keyIndent: 4, self.keyTitle: "title 01111", self.keyChildren: []],  [self.keyIndent: 4, self.keyTitle: "title 01112", self.keyChildren: []]]]]]]]]]]

我不确定你要找什么,但我会像这样设计模型类:

class MyModel
{
    var title: String?
    var children: [MyModel]?
    func buildDataModel(input: [MyModel]) -> [[String:Any]]?
    {
         // Dynamically build your array here, by walking input and its descendents.
         // keep track of the indent as you progress, so you can insert it to each dictionary as well.
    }
}

最新更新