包含字典数组的多维数组

时间:2013-12-11 13:17:53

标签: ios objective-c arrays

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

故事是字典的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"];
}

0 个答案:

没有答案