我有一個可以包含無限數量子項的CoreData模型。我想要顯示的每個對象的列表,縮進了可讀性,像這樣從coredata獲取父子數組
Object
Object first child
first childs children
first child children
Object second child
Object 2
Object also has children
MOre children
Childs
現在我來自一個PHP的背景。在PHP中,我會創建一個簡單的數組,它會遍歷一些函數來構建這個列表,但這對我來說似乎仍然很愚蠢。
我得到一個平面陣列,基本上有這樣的項目: array.name = @「Name」; array.children =無或coredata行 array.parent =無或coredata行
如何遍歷此並顯示列表縮進和分組像上面。
感謝任何指針或例子
着 - 下面指針完成了它:生成的代碼如下:
生成的代碼是(類似下面,我有我自己的調整,但多數民衆贊成細節)
- (NSArray *)flattenGroupsWithParent:(NSManagedObject<ECCGroup> *)parent {
//findGroupsForGroups gets all nodes with parent: parent.
NSArray *children = [dataSource findGroupsForGroup:parent];
for (NSManagedObject<ECCGroup> *child in children) {
ECCGroupNode *node = [[ECCGroupNode alloc] initWithGroup:child label:child.name];
[result addObject:node];
[result addObjectsFromArray:[self flattenGroupsWithParent:child]];
[node release];
}
}
生成的數組:結果。包含一個數組,按順序。與所有父母 - >孩子。 在我的情況下,在需要時縮進。 (使用額外的參數不是如上圖所示)