Obj C:将字符串分组为NSMutableDictionary

时间:2011-06-02 00:18:46

标签: objective-c ios nsmutablearray nsmutabledictionary

我有一个包含一堆字符串的NSArray。每个字符串都列出父类型和子类型,例如:

Tool: Saw
Fruit: Apple
Fruit: Orange
Tree: Maple
Fruit: Banana
Tree: Oak
Tool: Hammer
Tree: Cedar

我需要将它们放入的最终结构是(已排序的父母和孩子):

Fruit:
  - Apple
  - Banana
  - Orange
Tool:
  - Hammer
  - Saw
Tree:
  - Cedar
  - Maple
  - Oak

我想知道如何在父类型下对这些进行分组,按父类型排序并对子项进行排序。我假设使用的最好的东西是NSMutableDictionary,使它们成为键和对象。

这是我到目前为止测试的内容,但结果并不是我想要的:

// Add to tree
if ([tree objectForKey:pathParent]) {
    NSMutableArray *temp = (NSMutableArray *)[tree objectForKey:pathParent];
    [temp addObject:name];
    [tree removeObjectForKey:pathParent];
    [tree setObject:temp forKey:pathParent];

} else {
    [tree setObject:[NSMutableArray arrayWithObject:name] forKey:pathParent];
}//end

我该怎么做?

3 个答案:

答案 0 :(得分:0)

我认为最简单的方法是在原始列表上执行字母排序

Tool: Saw
Fruit: Apple
Fruit: Orange
Tree: Maple
Fruit: Banana
Tree: Oak
Tool: Hammer
Tree: Cedar

获得:

Fruit: Apple
Fruit: Banana
Fruit: Orange
Tool: Hammer 
Tool: Saw
Tree: Cedar
Tree: Maple
Tree: Oak

从那里你可以轻松地迭代数组并格式化它。查看第一个字符串的第一部分并打印出来然后换行第二部分。看看第二个字符串,如果它的第一个部分不同,你有一个新的父级,所以打印就像你做的第一个,否则只打印字符串的第二部分。以这种方式继续。

您还可以将元素存储在树中,这样您就不会拥有“父:节点”格式的字符串列表,而是拥有带子节点的节点树。这将使您能够拥有带子节点的子节点等。

第一个选项有效,但树数据结构是更容易扩展的更健壮的选项。

如果那些没有意义,请告诉我,我会更新澄清。

答案 1 :(得分:0)

[NVM,我看到你已经开始工作了!凉爽。]

大致相似的东西应该有用,你需要进行季节性调试才能尝试。但这是一般的想法......

- (NSDictionary*)categoriesAndSortedItemsFromItems:(NSArray*)source {

    //Given NSArray *source with the lines you described...

    NSMutableDictionary *categoriesAndItems = [NSMutableDictionary dictionaryWithCapacity:3];

    // Plug the items into the correct arrays by category
    for (NSString *line in source) {

        NSArray *components = [line componentsSeparatedByString:@": "];
        NSString *category = [components objectAtIndex:0]; //e.g. "tree"
        NSString *item = [components objectAtIndex:1]; //e.g. "oak"

        NSMutableArray *itemsForCategory = [self arrayInDict:categoriesAndItems forKey:category];

        [itemsForCategory addObject:item];
    }

    // Now run through and sort each array if you want...
    NSMutableDictionary *categoriesAndSortedItems = [NSMutableDictionary dictionaryWithCapacity:3];

    [categoriesAndItems enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

        NSArray *sorted = [(NSArray*)obj sortedArrayUsingSelector:@selector(compare:)];
        [categoriesAndSortedItems setObject:sorted forKey:key];
    }];

    return categoriesAndSortedItems;

}

 - (NSMutableArray*)arrayInDict:(NSMutableDictionary*)dict forKey:(NSString*)key {

        NSMutableArray *array = [dict objectForKey:key];

        if (!array)  {

            array = [[NSMutableArray alloc] initWithCapacity:10];

            [dict setObject:array forKey:key];

            [array release];
        }

        return array;
    }

答案 2 :(得分:0)

这是我在for循环中工作的内容:

if (pathParent != nil) {
    if ([tree objectForKey:pathParent]) {
        [[tree objectForKey:pathParent] addObject:name];
    } else {
        [tree setObject:[NSMutableArray arrayWithObject:name] forKey:pathParent];
    }//end
}//end