![](/img/trans.png)
[英]How to set NSURL Resource Values for all files and folders within a folder
[英]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.