【问题标题】:How get list of folders and files from resource folder in iPhone?如何从 iPhone 的资源文件夹中获取文件夹和文件列表?
【发布时间】:2011-12-08 20:02:11
【问题描述】:

我正在我的资源文件夹中做文件夹结构,例如... Resource => MyData => S1 然后在 S1=> Name.png, data.ppt

现在我想获取所有文件夹列表和文件名。这里 MyData 名称将只是静态的,其他可能会更改。就像我可以添加 S1、S2、S3 和其中的文件数。那么如何通过Objective C读取这些内容呢?我试过下面的代码。

NSString *bundlePathName = [[NSBundle mainBundle] bundlePath];
    NSString *dataPathName = [bundlePathName stringByAppendingPathComponent:@"Resources"];
    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);
    }

谢谢,

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    你可以像这样获取Resources目录的路径,

    NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
    

    然后将Documents追加到路径中,

    NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"];
    

    然后您可以使用NSFileManager 的任何目录列表API。

    NSError * error;
    NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];
    

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 2016-08-06
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2011-01-31
      相关资源
      最近更新 更多