组织 Xcode 项目中的文件并以编程方式获取文件夹列表
Organize files in Xcode project and get the list of folders programmatically
我在资源导航器的 Xcode 项目中有以下结构,我想在我的应用程序中反映出来。
也就是说,我想在 "Books" 文件夹中 "scan" 并获取其中所有文件夹的 NSArray
。下一步是我想获取每个文件夹中所有文件的 NSArray
。
到目前为止,我已经尝试使用连接到 NSBundle
的任何东西来获取文件夹列表,但这给了我错误的结果:
NSLog(@"bundle path is %@", [[NSBundle mainBundle] bundlePath]);
NSLog(@"resource path is %@", [[NSBundle mainBundle] resourcePath]);
这两种方法都不会以编程方式反映实际的文件夹结构。
我们有什么办法可以做到这一点吗?
正如@rmaddy 指出的那样,您实际上应该在 Xcode 项目中有 blue 文件夹,然后使用 NSDirectoryEnumerator
获取所有文件夹的完整列表文件夹。
这是我解决这个问题的方法:
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"Books" isDirectory:YES];
NSDirectoryEnumerator *dirEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:bundleURL includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey,nil] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants errorHandler:nil];
for (NSURL *theURL in dirEnumerator){
// Retrieve the file name. From NSURLNameKey, cached during the enumeration.
NSString *folderName;
[theURL getResourceValue:&folderName forKey:NSURLNameKey error:NULL];
// Retrieve whether a directory. From NSURLIsDirectoryKey cached during the enumeration.
NSNumber *isDirectory;
[theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
if([isDirectory boolValue] == YES){
NSLog(@"Name of dir is %@", folderName);
}
}
我在资源导航器的 Xcode 项目中有以下结构,我想在我的应用程序中反映出来。
也就是说,我想在 "Books" 文件夹中 "scan" 并获取其中所有文件夹的 NSArray
。下一步是我想获取每个文件夹中所有文件的 NSArray
。
到目前为止,我已经尝试使用连接到 NSBundle
的任何东西来获取文件夹列表,但这给了我错误的结果:
NSLog(@"bundle path is %@", [[NSBundle mainBundle] bundlePath]);
NSLog(@"resource path is %@", [[NSBundle mainBundle] resourcePath]);
这两种方法都不会以编程方式反映实际的文件夹结构。
我们有什么办法可以做到这一点吗?
正如@rmaddy 指出的那样,您实际上应该在 Xcode 项目中有 blue 文件夹,然后使用 NSDirectoryEnumerator
获取所有文件夹的完整列表文件夹。
这是我解决这个问题的方法:
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"Books" isDirectory:YES];
NSDirectoryEnumerator *dirEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:bundleURL includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey,nil] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants errorHandler:nil];
for (NSURL *theURL in dirEnumerator){
// Retrieve the file name. From NSURLNameKey, cached during the enumeration.
NSString *folderName;
[theURL getResourceValue:&folderName forKey:NSURLNameKey error:NULL];
// Retrieve whether a directory. From NSURLIsDirectoryKey cached during the enumeration.
NSNumber *isDirectory;
[theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
if([isDirectory boolValue] == YES){
NSLog(@"Name of dir is %@", folderName);
}
}