回答
我还没有发现,我可以说是简单的做&,最后我不得不写我自己的代码来做到这一点的任何事情。我在这里张贴这个,因为也许有人找到这个帮助完整。
-(void)search{
@autoreleasepool {
NSString *baseDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSFileManager *defFM = [NSFileManager defaultManager];
BOOL isDir = YES;
NSArray *fileTypes = [[NSArray alloc] initWithObjects:@"mp3",@"mp4",@"avi",nil];
NSMutableArray *mediaFiles = [self searchfiles:baseDir ofTypes:fileTypes];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"playlist.plist"];
if(![defFM fileExistsAtPath:filePath isDirectory:&isDir]){
[defFM createFileAtPath:filePath contents:nil attributes:nil];
}
NSMutableDictionary *playlistDict = [[NSMutableDictionary alloc]init];
for(NSString *path in mediaFiles){
NSLog(@"%@",path);
[playlistDict setValue:[NSNumber numberWithBool:YES] forKey:path];
}
[playlistDict writeToFile:filePath atomically:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshplaylist" object:nil];
}
}
现在给定的应用程序沙箱递归方法
-(NSMutableArray*)searchfiles:(NSString*)basePath ofTypes:(NSArray*)fileTypes{
NSMutableArray *files = [[[NSMutableArray alloc]init] autorelease];
NSFileManager *defFM = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *dirPath = [defFM contentsOfDirectoryAtPath:basePath error:&error];
for(NSString *path in dirPath){
BOOL isDir;
path = [basePath stringByAppendingPathComponent:path];
if([defFM fileExistsAtPath:path isDirectory:&isDir] && isDir){
[files addObjectsFromArray:[self searchfiles:path ofType:fileTypes]];
}
}
NSArray *mediaFiles = [dirPath pathsMatchingExtensions:fileTypes];
for(NSString *fileName in mediaFiles){
fileName = [basePath stringByAppendingPathComponent:fileName];
[files addObject:fileName];
}
return files;
}
看到使用NSFileManager
你可以得到的文件和波纹管的条件与你能得到特别的扩展,它在文档目录的工作文件..
-(NSArray *)findFiles:(NSString *)extension
{
NSMutableArray *matches = [[NSMutableArray alloc]init];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *item;
NSArray *contents = [manager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil];
for (item in contents)
{
if ([[item pathExtension]isEqualToString:extension])
{
[matches addObject:item];
}
}
return matches;
}
使用这个数组与搜索文件..获得NSArray
类型的返回,以便使用NSArray
对象来存储这些数据...
我希望这对您有所帮助...
@AmjadKhan也看到此链接http://stackoverflow.com/questions/11646325/get-files-inside-a-folder-in-document-directory-in-iphone –
感谢您的回复,但我知道这与我也问题是我有多个目录和子目录。如果我这样做,它可能需要一点时间来处理所有文件。我正在寻找更快的东西。 –
@AmjadKhan看到这个链接http://stackoverflow.com/questions/1567134/how-can-i-get-a-writable-path-on-the-iphone,你也可以改变字典名称,也是设置你需要的路径只是试试吧.. –
是的,我们有直接的方法,下面的NSArray帮助你
NSMutableArray *arrayFiles = [[NSMutableArray alloc] initWithObjects:@"a.png", @"a.jpg", @"a.pdf", @"h.png", @"f.png", nil];
NSLog(@"pathsMatchingExtensions----%@",[arrayFiles pathsMatchingExtensions:[NSArray arrayWithObjects:@"png", nil]]);
//my output is
"a.png",
"h.png",
"f.png"
喜欢这种方式,您可以找到您的特定文件扩展名
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];
NSString *filename;
while ((filename = [direnum nextObject])) {
if ([filename hasSuffix:@".doc"]) { //change the suffix to what you are looking for
[arrayListofFileName addObject:[filename stringByDeletingPathExtension]];
}
}
你需要的是一个递归方法,这样就可以处理子目录。以下第一种方法是公开的;另一个私人。想象自己被实现为称为类的静态方法CocoaUtil
:
CocoaUtil.h:
@interface CocoaUtil : NSObject
+ (NSArray *)findFilesWithExtension:(NSString *)extension
inFolder:(NSString *)folder;
@end
CocoaUtil.m:
// Private Methods
@interface CocoaUtil()
+ (NSArray *)_findFilesWithExtension:(NSString *)extension
inFolder:(NSString *)folder
andSubFolder:(NSString *)subFolder;
@end
@implementation CocoaUtil
+ (NSArray *)findFilesWithExtension:(NSString *)extension
inFolder:(NSString *)folder
{
return [CocoaUtil _findFilesWithExtension:extension
inFolder:folder
andSubFolder:nil];
}
+ (NSArray *)_findFilesWithExtension:(NSString *)extension
inFolder:(NSString *)folder
andSubFolder:(NSString *)subFolder
{
NSMutableArray *found = [NSMutableArray array];
NSString *fullPath = (subFolder != nil) ? [folder stringByAppendingPathComponent:subFolder] : folder;
NSFileManager *fileman = [NSFileManager defaultManager];
NSError *error;
NSArray *contents = [fileman contentsOfDirectoryAtPath:fullPath error:&error];
if (contents == nil)
{
NSLog(@"Failed to find files in folder '%@': %@", fullPath, [error localizedDescription]);
return nil;
}
for (NSString *file in contents)
{
NSString *subSubFolder = subFolder != nil ? [subFolder stringByAppendingPathComponent:file] : file;
fullPath = [folder stringByAppendingPathComponent:subSubFolder];
NSError *error = nil;
NSDictionary *attributes = [fileman attributesOfItemAtPath:fullPath error:&error];
if (attributes == nil)
{
NSLog(@"Failed to get attributes of file '%@': %@", fullPath, [error localizedDescription]);
continue;
}
NSString *type = [attributes objectForKey:NSFileType];
if (type == NSFileTypeDirectory)
{
NSArray *subContents = [CocoaUtil _findFilesWithExtension:extension inFolder:folder andSubFolder:subSubFolder];
if (subContents == nil)
return nil;
[found addObjectsFromArray:subContents];
}
else if (type == NSFileTypeRegular)
{
// Note: case sensitive comparison!
if ([[fullPath pathExtension] isEqualToString:extension])
{
[found addObject:fullPath];
}
}
}
return found;
}
@end
这将返回一个包含完整路径的每一个数组文件与指定的文件扩展名。需要注意的是[NSString pathExtension]
不返回文件扩展名的.
所以一定不要通过,在extension
参数。
用NSArray替换NSArray * subContents = ...... line subContents = [CocoaUtil _findFilesWithExtension:extension inFolder:folder andSubFolder:subSubFolder]; 它工作正常。谢谢! – Vrasidas
使用下面的代码
NSArray *myFiles = [myBundle pathsForResourcesOfType:@"Your File extension"
inDirectory:nil];
- 1. C#扩展方法与特定属性
- 2. 目标-c中扩展的变量
- 3. 扩展与C#
- 4. 与扩展斯威夫特扩展
- 5. 夫特:定义与目标C块
- 6. SaveFileDialog显示仅与特定扩展名
- 7. 标签目标扩展器
- 8. 从目标C调用在Swift中定义的NSString扩展名
- 9. 我的C++扩展与faulthandler
- 10. 创建一个自定义数据扩展与精确目标
- 11. .pro扩展在C++项目
- 12. StaticResource标记扩展与System.Windows.Application.FindResource
- 13. distutils.core VS与C++扩展
- 14. 扩展Python 3与C++
- 15. 目标C与目标C++
- 16. jQuery的 - 事件扩展得到事件的目标或特定类型
- 17. 用特定的扩展名将文件重写到特定的目录
- 18. Chrome扩展图标://扩展/
- 19. 检查具有特定扩展名的文件的目录
- 20. 在当前目录中搜索特定扩展名的文件C
- 21. 科特林:如何与扩展功能扩展枚举类
- 22. Gitignore - 如何在特定目录中排除特定扩展名的文件?
- 23. 与jQuery的目标特定格
- 24. 扩展方法的空目标
- 25. 7.1兼容目标的WatchKit扩展:WKInterfaceLabel
- 26. makefile中通配符扩展的目标
- 27. 将具有特定扩展名的文件移动到目录
- 28. 如何过滤TShellListView项目到特定的文件扩展名?
- 29. django从django.po扩展特定邮件的javascript翻译目录
- 30. 替换特定目录内的文件扩展名
哪里寻找这些文件,会阻止您寻找在大多数地方? – trojanfoe
我想搜索NSDocumentDirectory中的文件。 –
@AmjadKhan使用'NSFileManager'类。它可以返回指定目录中的所有文件名。 – 2013-01-01 12:09:56