簡體   English   中英

如何獲取iPhone資源文件夾中的文件夾和文件列表?

[英]How get list of folders and files from resource folder in iPhone?

我在我的資源文件夾中做文件夾結構,比如... 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);
    }

謝謝,

您可以像這樣獲取Resources目錄的路徑,

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

然后將Documents附加到路徑,

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

然后,您可以使用NSFileManager任何目錄列表API。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM