Reputation: 909
I know there is a somewhat simple solution here, I can't quite wrap my head around it though. I have a user generated .plist file that can have a (reasonable) infinite amount of nested trees in it. What I'm wanting to do is create a folder structure based on how the plist is nested.
Since I do not know how deep the main dictionary goes I cant figure out how to loop through it and tell it to create a directory then dive into that and create directories within it.
I know how to create folders its iterating through the entire list is what is throwing me off. I'm pretty sure I need a separate recursive method just am not sure where to begin. Key "Children" and "Name" are what I'm using to create the list. Any help would be great.
Upvotes: 0
Views: 1328
Reputation: 1190
May be this function will help you,
void recursiveCall (NSDictionary *dictionary) {
NSArray *_keys = [dictionary allKeys];
for (NSString *_key in _keys) {
id obj = [dictionary valueForKey:_key];
NSLog(@"Create folder of %@",_key);
if ([obj isKindOfClass:[NSArray class]]) {
for (id _obj in obj) {
if ([_obj isKindOfClass:[NSDictionary class]]) {
recursiveCall(_obj);
}
else {
NSLog(@"Create folder of %@",_obj);
}
}
}
}
}
Upvotes: 1
Reputation: 3020
I'd start with defining a path to the directory where you want to create the directory structure. Then call the function to build the directory structure.
NSString *pathToDirectory = @"./";
[self buildDirectoriesAtPath: pathToDirectory fromDictionary: dictionary];
The function -buildDirectoriesAtPath:fromDictionary:
is recursive and would look something like this:
- (void) buildDirectoriesAtPath: (NSString *) filepath fromDictionary: (NSDictionary *) dictionary {
for (NSString *key in dictionary.allKeys) {
if ([key isEqualToString: @"Children"]) {
NSArray *children = [dictionary objectForKey: key];
for (id object in children) {
if ([object isKindOfClass: [NSDictionary class]]) {
NSDictionary *directoryInfo = (NSDictionary *) object;
NSString *directoryName = [directoryInfo objectForKey: @"Name"];
NSString *directoryPath = [NSString stringWithFormat: @"%@%@", filepath, directoryName];
// Create directory
NSLog(@"Creating directory: %@", directoryPath);
// Create subdirectories
NSArray *subdirectories = [directoryInfo objectForKey: @"Children"];
for (id directory in subdirectories) {
if ([directory isKindOfClass: [NSString class]]) {
NSString *subdirectoryName = (NSString *) directory;
NSString *subdirectoryPath = [NSString stringWithFormat: @"%@/%@", directoryPath, subdirectoryName];
// Create directory
NSLog(@"Creating directory: %@", subdirectoryPath);
}
else if ([directory isKindOfClass: [NSDictionary class]]) {
NSDictionary *subdirectory = (NSDictionary *) directory;
NSString *subdirectoryName = [subdirectory objectForKey: @"Name"];
NSString *subdirectoryPath = [NSString stringWithFormat: @"%@/%@/", directoryPath, subdirectoryName];
[self buildDirectoriesAtPath: subdirectoryPath fromDictionary: (NSDictionary *) directory];
}
}
}
else if ([object isKindOfClass: [NSString class]]) {
NSString *directoryName = (NSString *) object;
NSString *directoryPath = [NSString stringWithFormat: @"%@%@", filepath, directoryName];
// Create directory
NSLog(@"Creating directory: %@", directoryPath);
}
}
}
}
}
If there's anything you'd like clarified in this, don't hesitate to ask. :)
Upvotes: 1