帮助:列出iPhone应用程序包子路径的内容?

时间:2010-05-20 13:37:25

标签: iphone

我正在尝试获取应用程序包的子路径中包含的目录列表。我做了一些搜索,这就是我想出来的

- (void) contents {
 NSArray *contents = [[NSBundle mainBundle] pathsForResourcesOfType:nil
                                               inDirectory:@"DataDir"];
 if (contents = nil) {
  NSLog(@"Failed: path doesn't exist or error!");
 } else {
     NSString *bundlePathName = [[NSBundle mainBundle] bundlePath];
  NSString *dataPathName = [bundlePathName stringByAppendingPathComponent:
                                 @"DataDir"];
     NSFileManager *fileManager = [NSFileManager defaultManager];

     NSMutableArray *directories = [[NSMutableArray alloc] init];
     for (NSString *entityName in contents) {
            NSString *fullEntityName = [dataPathName
                                       stringByAppendingPathComponent:entityName];
            NSLog(@"entity = %@", fullEntityName);
            BOOL isDir = NO;
            [fileManager fileExistsAtPath:fullEntityName isDirectory:(&isDir)];
            if (isDir) {
                [directories addObject:fullEntityName];
                NSLog(@" is a directory");
            } else {
                NSLog(@" is not a directory");
            }
        }
        NSLog(@"Directories = %@", directories);

        [directories release];
 }
}

正如您所看到的,我正在尝试获取应用程序包的DataDir子路径中的目录列表。问题是我的内容NSArray中没有字符串。

请注意:
- 我正在使用模拟器
- 当我手动查看.app文件时,我可以看到DataDir及其中的内容 - DataDir的内容是包含png文件的png文件和目录 - 应用程序逻辑需要在运行时发现DataDir的内容 - 我也尝试过使用

 NSArray *contents = [fileManager contentsOfDirectoryAtPath:DataDirPathName error:nil];

我的内容数组

中仍然没有任何条目

任何建议/替代方法?

感谢。

1 个答案:

答案 0 :(得分:2)

我不确定我昨天做错了什么,但是我的代码工作正常:

NSString *bundlePathName = [[NSBundle mainBundle] bundlePath];
NSString *dataPathName = [bundlePathName stringByAppendingPathComponent:@"DataDir"];
NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:dataPathName]) {
        NSLog(@"%@ exists", dataPathName);
        BOOL isDir = NO;
        [fileManager fileExistsAtPath:dataPathName isDirectory:(&isDir)];
        if (isDir == YES) {
            NSLog(@"%@ is a directory", dataPathName);
            NSArray *contents;
            contents = [fileManager contentsOfDirectoryAtPath:dataPathName error:nil];
            for (NSString *entity in contents) {
                NSLog(@"%@ is within", entity);
            }
        } else {
            NSLog(@"%@ is not a directory", dataPathName);
        }
    } else {
        NSLog(@"%@ does not exist", dataPathName);
    }