开发者

Help: List contents of iPhone application bundle subpath?

开发者 https://www.devze.com 2022-12-31 23:10 出处:网络
I am trying to get a listing of the directories contained within a subpath of my application bundle.I\'ve done some searching and this is what I have come up with

I am trying to get a listing of the directories contained within a subpath of my application bundle. I've done some searching and this is what I have come up with

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

As开发者_开发知识库 you can see I am trying to get a listing of directories in the app bundle's DataDir subpath. The problem is that I get no strings in my contents NSArray.

note:

- I am using the simulator

- When I manually look in the .app file I can see DataDir and the contents therein

- The contents of DataDir are png files and directories that contain png files

- The application logic needs to discover the contents of DataDir at runtime

- I have also tried using

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

and I still get no entries in my contents array

Any suggestions/alternative approaches?

Thanks.


I'm not sure what I was doing wrong yesterday but I have this code working:

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

精彩评论

暂无评论...
验证码 换一张
取 消