简体   繁体   English

将目录内容复制到NSArray

[英]Copy contents of directory to an NSArray

I have many images of jpg format in a folder named Wallpaper . 我在名为Wallpaper的文件夹中有许多jpg格式的图像。 I am trying to read the contents of directory and store them in an array using the following code : 我正在尝试读取目录的内容,并使用以下代码将它们存储在数组中:

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:@"/Wallpaper" error:NULL];

but its not working. 但它不起作用。 I am unable to fetch them into the array. 我无法将它们提取到阵列中。 Can anyone tell me what's wrong with this ? 谁能告诉我这是怎么了?

You need to specify the complete path of the directory. 您需要指定目录的完整路径。 I assume your folder Wallpaper is in the Documents Directory. 我假设您的文件夹Wallpaper位于文档目录中。

NSFileManager   *fileManager        =   [NSFileManager defaultManager];
NSString        *documentsPath      =   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString        *extractDirPath     =   [documentsPath stringByAppendingString: @"/Wallpaper"];
NSArray         *extractsList       =   [fileManager contentsOfDirectoryAtPath: extractDirPath error: nil];

You are using absolute path "/Wallpaper", but I guess your wallpaper folder is not in /? 您正在使用绝对路径“ / Wallpaper”,但是我想您的墙纸文件夹不在/?中。

NSArray *fileList = [manager contentsOfDirectoryAtPath:@"/Wallpaper" error:NULL];

And try it like this: 并尝试这样:

NSError *error = nil;
NSArray *fileList = [manager contentsOfDirectoryAtPath:@"/Wallpaper" error:&error];
NSLog(@"load wallpaper error: %@", error);

This will give you the actual error occurred. 这将为您提供实际发生的错误。

Try This: 尝试这个:

NSFileManager *filemgr= [NSFileManager defaultManager];

NSArray *filelist = [filemgr contentsOfDirectoryAtPath:Pathwithfoldername error:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM