如何使用多级创建NSDictionary

时间:2014-04-09 09:30:59

标签: ios objective-c macos nsdictionary nsmutabledictionary

您能帮我创建以下结构的NSDictionary格式吗?

  Children =     (
            {
        Children =             (
                            {
                Children =                     (
                                            {
                        Children =                             (
                            Monty,
                            Smithers,
                            Frink,
                            Carl,
                            Cletus,
                            Bobo,
                            Duff
                        );
                        Name = Simpsons;
                    },
                                            {
                        Children =                             (
                            "Mountain Unicycling",
                            "Rock Climbing",
                            Bouldering,
                            Snowboarding
                        );
                        Name = Sports;
                    },
                                            {
                        Children =                             (
                            "Eating Strawberries",
                            Mangoes,
                            Treehouses,
                            "Good Weather",
                            Maui,
                            Trogdor
                        );
                        Name = "Just Fun Stuff";
                    },
                    "Foo with the Bar",
                    Bar,
                    BooFar,
                    "Foo Bar"
                );
                Name = Shows;
            },
                            {
                Children =                     (
                                            {
                        Children =                             (
                            "Big Sur",
                            "New York",
                            Naples,
                            "Chez Panisse",
                            Lucia,
                            Switzerland,
                            Ireland
                        );
                        Name = Vacation;
                    },
                                            {
                        Children =                             (
                            "Green Bay",
                            Milwaukee,
                            Monroe
                        );
                        Name = Other;
                    }
                );
                Name = Travel;
            },
                            {
                Children =                     (
                    Kark,
                    Valen,
                    Kosh,
                    "Red Letter",
                    "Fluffy Ears",
                    Hiro
                );
                Name = "Science Fiction";
            }
        );
        Name = "Favorite Things Group";
    },
            {
        Children =             (
            Mischief,
            Mayhem,
            Soap
        );
        Name = "Movies Group with a Long Title";
    },
            {
        Children =             (
            ObjC,
            Cocoa,
            KVC,
            NSTooblar
        );
        Name = "Things That Rule";
    },
            {
        Name = Eagle;
    }
  );
  Name = OVRoot;
}

我必须尝试很多方法。我仍然在努力使用NSDictionary代码。这是我的代码。

  NSArray *desk = [NSArray arrayWithObjects:@"Desktop",@"Mac App",@"IOS App"];
 NSMutableDictionary *secondtype = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Desktop",@"Name",
                                [NSMutableArray arrayWithArray:desk],@"Children",nil];


 NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"OVRoot",@"Name",
                                [NSMutableArray arrayWithObjects:secondtype,
                                nil], @"Children",nil];

您能否帮助我了解如何动态创建此层次结构。在我的项目范围内显示桌面,以树格式记录所有文件夹名称。

2 个答案:

答案 0 :(得分:0)

NSDictionary仅用于键值PAIRS。它并不意味着像数据库那样行事,而是以你试图这样做的方式滥用。您是否考虑过使用其他类型的数据结构?

答案 1 :(得分:0)

正如Tander所说,我不会在这里使用NSDictionary(尽管可能......)。

尝试根据需要创建新的数据类型,而不是破坏字典。 类似的东西:

@interface MyFolder
    @property(nonatomic) NSString* folderName;
    @property(nonatomic, strong) NSArray* subFolders;

    //more relevant members/actions here... 

@end

您还可以创建一个静态工厂方法,将字符串反序列化为' MyFolder'实例(当然会递归..)

祝你好运