如何使用带文件路径的NSArray获取NSDictionary?

时间:2013-12-07 17:36:24

标签: ios nsarray nsdictionary filetree

我有一个看起来像这样的NSArray:

@[@"file1", @"directory/testfile", @"directory/otherdir/testfile", @"otherdir/", @"otherdir/otherfile"]

我正在尝试将其转换为NSDictionary,如下所示:

 @{
  @"directory/":@{
    @"otherdir/":@{
      @"testfile":[..some array with data..]}
    },
    @"testfile":[..some array with data..]
  },
  @"otherdir/":@{
    @"otherfile":[..some array with data..]
  },
  @"file1":[..some array with data..]
}

基本上,NSDictionary看起来像文件树。提前谢谢!

编辑:我拥有的实际数组是:

(
    "blog/",
    "blog/code.css",
    "blog/style.css",
    "etc/",
    "etc/awsservices.png",
    "etc/speeds.png",
    "etc/test/",
    "etc/test/other/",
    "etc/test/other/speeds.png",
    "site/",
    "site/dino.png",
    "site/error.css",
    "site/main.css",
    "site/signet.min.js"
)

我为此写的代码:

-(NSDictionary*)buildDictionaryWithContentsOfPath:(NSMutableArray*)files {
    NSString *subDir;
    NSMutableDictionary *returnable = [[NSMutableDictionary alloc] initWithDictionary:@{}];
    BOOL directory;

    NSLog(@"%@", files);

    for (S3ObjectSummary *objectSummary in files) {

        if ([[objectSummary key] hasSuffix:@"/"]) {

            if (subDir && [[objectSummary key] hasPrefix:subDir]) {
                NSLog(@"prefixed %@", [objectSummary key]);
                NSMutableDictionary *treeDict = [[NSMutableDictionary alloc] initWithDictionary:@{}];

                [returnable[subDir] setObject:treeDict forKey:[objectSummary key]];

            } else {
                subDir = [objectSummary key];
                NSLog(@"dir %@", [objectSummary key]);
                NSMutableDictionary *treeDict = [[NSMutableDictionary alloc] initWithDictionary:@{}];
                [returnable setObject:treeDict forKey:[objectSummary key]];
            }
            directory = true;

        } else {
            S3GetObjectMetadataRequest *getMetadataRequest = [[S3GetObjectMetadataRequest alloc] initWithKey:[objectSummary key] withBucket:self.bucket.name];
            S3GetObjectMetadataResponse *getMetadataResponse = [self.client getObjectMetadata:getMetadataRequest];

            if (![[objectSummary key] hasPrefix:subDir]) {
                NSLog(@"file %@",[objectSummary key]);
                [returnable setObject:@{@"filename":[objectSummary key], @"directory":@(directory), @"created_at":getMetadataResponse.lastModified} forKey:[objectSummary key]];
            } else {
                for (NSString __strong *pathComp in [[objectSummary key] pathComponents]) {
                    pathComp  = [NSString stringWithFormat:@"%@/", pathComp];
                    NSLog(@"path component %@", pathComp);
                    if (![[objectSummary key] hasSuffix:@"/"]) {
                        if (returnable[pathComp]) {
                            NSLog(@"has comp %@", pathComp);
                            [returnable[pathComp] setObject:@{@"filename":[objectSummary key], @"directory":@(directory), @"created_at":getMetadataResponse.lastModified} forKey:[objectSummary key]];
                        }
                    }

                }

            }
            directory = false;
        }



        //[self.fileTree addObject:@{@"filename":[objectSummary key], @"directory":@(directory), @"created_at":getMetadataResponse.lastModified}];

    }
    return returnable;

}

它返回的字典:

{
    "blog/" =     {
        "blog/code.css" =         {
            "created_at" = "2013-12-07 12:13:37 +0000";
            directory = 0;
            filename = "blog/code.css";
        };
        "blog/style.css" =         {
            "created_at" = "2013-12-07 12:13:36 +0000";
            directory = 0;
            filename = "blog/style.css";
        };
    };
    "etc/" =     {
        "etc/awsservices.png" =         {
            "created_at" = "2013-12-07 12:26:37 +0000";
            directory = 0;
            filename = "etc/awsservices.png";
        };
        "etc/speeds.png" =         {
            "created_at" = "2013-12-07 13:29:27 +0000";
            directory = 0;
            filename = "etc/speeds.png";
        };
        // PROBLEM HERE
        "etc/test/" =         {
        };
        "etc/test/other/" =         {
        };
        "etc/test/other/speeds.png" =         {
            "created_at" = "2013-12-07 17:29:21 +0000";
            directory = 0;
            filename = "etc/test/other/speeds.png";
        };
        // PROBLEM HERE
    };
    "site/" =     {
        "site/dino.png" =         {
            "created_at" = "2013-12-07 12:13:59 +0000";
            directory = 0;
            filename = "pmerino/dino.png";
        };
        "site/error.css" =         {
            "created_at" = "2013-12-07 12:13:59 +0000";
            directory = 0;
            filename = "pmerino/error.css";
        };
        "site/main.css" =         {
            "created_at" = "2013-12-07 12:13:59 +0000";
            directory = 0;
            filename = "pmerino/main.css";
        };
        "site/signet.min.js" =         {
            "created_at" = "2013-12-07 12:13:59 +0000";
            directory = 0;
            filename = "pmerino/signet.min.js";
        };
    };
}

如您所见,这部分:

"etc/test/" =         {
};
"etc/test/other/" =         {
};
"etc/test/other/speeds.png" =         {
    "created_at" = "2013-12-07 17:29:21 +0000";
    directory = 0;
    filename = "etc/test/other/speeds.png";
};

应该是这样的:

"etc/" =         {
  "test/" =         {
    "other/" = {
      "speeds.png" =         {
          "created_at" = "2013-12-07 17:29:21 +0000";
          directory = 0;
          filename = "etc/test/other/speeds.png";
      };
    };
  };
};

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,这种方法应该会有所帮助:

// Add the path to the tree. Returns the "node" for this path.
-(NSMutableDictionary *)addFile:(NSString *)path toTree:(NSMutableDictionary *)tree
{
    NSMutableDictionary *node = tree;

    // Split path into components:
    NSArray *components = [path componentsSeparatedByString:@"/"];

    // Create all intermediate dictionaries:
    for (NSUInteger i = 0; i < [components count] - 1; i++) {
        NSString *key = [components[i] stringByAppendingString:@"/"];
        if (node[key] == nil) {
            node[key] = [NSMutableDictionary dictionary];
        }
        node = node[key];
    }

    // Process last component, which is the file name,
    // or "" in the case of a dictionary:
    NSString *name = [components lastObject];
    if ([name length] > 0) {
        node[name] = [NSMutableDictionary dictionary];
        node = node[name];
    }
    return node;
}

如果你这样使用它:

NSArray *files = ...; // Your array of files
NSMutableDictionary *tree = [NSMutableDictionary dictionary];
for (NSString *path in files) {
    NSMutableDictionary *node = [self addFile:path toTree:tree];
    BOOL isDir = [path hasSuffix:@"/"];
    if (!isDir) {
        node[@"filename"] = path;
    }
}

结果字典是

{
    "blog/" =     {
        "code.css" =         {
            filename = "blog/code.css";
        };
        "style.css" =         {
            filename = "blog/style.css";
        };
    };
    "etc/" =     {
        "awsservices.png" =         {
            filename = "etc/awsservices.png";
        };
        "speeds.png" =         {
            filename = "etc/speeds.png";
        };
        "test/" =         {
            "other/" =             {
                "speeds.png" =                 {
                    filename = "etc/test/other/speeds.png";
                };
            };
        };
    };
    "site/" =     {
        "dino.png" =         {
            filename = "site/dino.png";
        };
        "error.css" =         {
            filename = "site/error.css";
        };
        "main.css" =         {
            filename = "site/main.css";
        };
        "signet.min.js" =         {
            filename = "site/signet.min.js";
        };
    };
}