Question

Can you help me how to create NSDictionary format of the following structure.

  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;
}

I have to tried many ways. Still i struggle with the NSDictionary code. This is my code is following.

  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];

Could you pls help me to how to create this hierarchy as dynamically. In my project scope is display the Desktop, document all folder name in tree format.

Was it helpful?

Solution

as Tander mentioned I wouldn't use here a NSDictionary (eventhough it possible...).

Try to create a new data-type for your needs rather than compromise the dictionary. something like:

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

    //more relevant members/actions here... 

@end

you may also create a static factory method to deserialize a string into 'MyFolder' instance (which will be recursive of course..)

Good Luck

OTHER TIPS

NSDictionary is for key-value PAIRS only. Its not meant to act like a database and abused in the way you're trying to do so. Have you considered using another type of data structor for this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top