Ios 包含字典数组的多维数组

Ios 包含字典数组的多维数组,ios,objective-c,arrays,Ios,Objective C,Arrays,通过我的ios第一个应用程序开发,我必须对一个包含字典的数组进行重新排序,该数组从xml文档中解析而来,重新排序的目的是将其发送给一个构建可折叠的函数,因此它需要一个childCell索引和一个parentCell索引来打印每个子元素的字符串,然后传递给另一个父元素。这里的问题是:我能够填充包含字典数组的大数组,然后我填充该数组并循环填充childArray以包含多个字典,然后我将该子数组添加到父数组中,每件事情似乎都在运行,但最后它给了我一个空数组。我将我的代码用于向您展示我是如何尝试这样做的

通过我的ios第一个应用程序开发,我必须对一个包含字典的数组进行重新排序,该数组从xml文档中解析而来,重新排序的目的是将其发送给一个构建可折叠的函数,因此它需要一个childCell索引和一个parentCell索引来打印每个子元素的字符串,然后传递给另一个父元素。这里的问题是:我能够填充包含字典数组的大数组,然后我填充该数组并循环填充childArray以包含多个字典,然后我将该子数组添加到父数组中,每件事情似乎都在运行,但最后它给了我一个空数组。我将我的代码用于向您展示我是如何尝试这样做的:

stories是字典的NSArray,childArray是应该包含故事字典的数组,parentArray是包含所有内容的数组。 如果有人谁已经这样做了,可以解释我是错的,请它将非常感谢

-(NSMutableArray *)orderChildsAndParents:(NSMutableArray *)fromArray
{
    int varial = 0;
    int catIndex = 0;

    NSMutableArray *parentArray = [NSMutableArray array];

    while(varial < [stories count])
    {
        NSString* cleanedString = [[[[stories objectAtIndex:varial] objectForKey:@"category"] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
                                   componentsJoinedByString:@""];

        if ([cleanedString isEqualToString:[category objectAtIndex:catIndex] ])
        {
            if (!childArray || !childArray.count)
            childArray = [NSMutableArray array];

            [childArray addObject:[stories objectAtIndex:varial]];
            varial++;
        }
        else{
            [parentArray addObject:childArray];
            [childArray removeAllObjects];
            catIndex++;
        }
    }
    NSLog(@"%@", parentArray);
    return parentArray;
}

- (NSString *) labelForCellAtChildIndex:(NSInteger) childIndex withinParentCellIndex:(NSInteger) parentIndex {

    NSMutableArray *orderedArray = [self orderChildsAndParents:stories];
    NSLog(@"format string %@",  [[[orderedArray objectAtIndex:parentIndex] objectAtIndex:childIndex] objectForKey:@"name"]); // empty :8
    return [[[orderedArray objectAtIndex:parentIndex] objectAtIndex:childIndex] objectForKey:@"name"];
}
-(NSMutableArray*)OrderChilds和Parents:(NSMutableArray*)fromArray
{
整变量=0;
int-catIndex=0;
NSMutableArray*parentArray=[NSMutableArray];
而(变量<[故事计数])
{
NSString*cleanedString=[[[[stories objectAtIndex:varial]objectForKey:@“category”]组件由字符分隔集:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
组件通过字符串连接:@“];
if([cleanedString IsequalString:[category objectAtIndex:catIndex]])
{
如果(!childArray | |!childArray.count)
childArray=[NSMutableArray];
[childArray addObject:[stories objectAtIndex:varial]];
变量++;
}
否则{
[parentArray addObject:childArray];
[childArray removeAllObjects];
catIndex++;
}
}
NSLog(@“%@”,父数组);
返回父数组;
}
-(NSString*)LabelForcellateChildIndex:(NSInteger)具有InParentCellindex:(NSInteger)parentIndex的childIndex{
NSMutableArray*orderedArray=[self-orderChildsAndParents:stories];
NSLog(@“格式字符串%@”,[[orderedArray objectAtIndex:parentIndex]objectAtIndex:childIndex]objectForKey:@“名称”];//空:8
返回[[[orderedArray objectAtIndex:parentIndex]objectAtIndex:childIndex]objectForKey:@“name”];
}

由于该方法的逻辑取决于该方法外部的许多内容,因此我建议使用调试器或
NSLog
来确定代码采用哪些分支。是的,我在调试器上,我的parentArray在else语句的第一个go中填充,然后在第二次运行int else时它变为空,添加childArray但其中没有数据:8,我必须在某个地方做一些非常错误的事情。只是问一下,我可以选择添加新对象(如addObjectAtIndex)的索引吗(不存在)?调试器只是告诉我,当我从childArray中移除AllObject时,它会从parentArray中移除它们,有没有一种方法可以添加一个对象,但作为一个副本,实际上并没有添加对象???很抱歉,我问了很多问题,但这是为了澄清所有这些。我怀疑您需要做的是在填充完要给
parentArray
的对象后创建一个新的
childArray
,而不是删除它的对象。