2
A
回答
8
试试这个:
//Retrieve an array of paths in the given directory
NSArray *imagePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: @"/Images/" error: NULL];
if(imagePaths==nil || [imagePaths count]==0) {
//Path either does not exist or does not contain any files
return;
}
NSEnumerator *enumerator = [imagePaths objectEnumerator];
id imagePath;
NSString *newImageName;
NSMutableArray *imageNames = [[NSMutableArray alloc] init];
while (imagePath = [enumerator nextObject]) {
//Remove the file extension
newImageName = [imagePath stringByDeletingPathExtension];
[imageNames addObject:newImageName];
}
代码的第一部分检索给定目录中文件的名称,并将它们添加到imagePaths
阵列中。
代码的第二部分则循环通过阵列中的图像路径和从端部(使用stringByDeletingPathExtension
方法)去除扩展,将它们附加到imageNames
阵列,其中将包含在给定目录中的所有文件没有任何文件扩展名。
由于Graham Lee
已在他的代码中完成,因此您可以提供一个指向NSError
对象的指针,以便在出现错误时获取有关该问题的信息。
4
NSError *error = nil;
NSArray *imageFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: @"/path/to/Images/" error: &error];
if (imageFiles == nil) //handle the error
相关问题
- 1. 获取文件的扩展名,但无法获取文件名
- 2. 如何从NSArray获取名称
- 3. 获取文件名
- 4. 获取文件名
- 5. 获取文件名
- 6. 从NSArray获取单个属性的NSArray
- 7. 获取文件的名称
- 8. 从NSArray获取NSString
- 9. 获取NSArray键值
- 10. 获取NSArray JSON值
- 11. 获取加密文件的文件名
- 12. 获取PHP文件的文件名
- 13. 获取文件路径的文件名
- 14. 获取整数值的NSArray
- 15. 从NSArray中获取的NSIndexSet
- 16. 获取NSArray中的xcode项目文件夹的所有图像
- 17. 7zip - 获取提取文件的名称
- 18. 获取文件名批量
- 19. ASP.NET:获取ASPX文件名
- 20. 获取BPL文件名
- 21. 动态获取文件名
- 22. ASPxUploadControl FileUploadComplete获取文件名
- 23. jwplayer 6:获取文件名
- 24. 从BufferedImage获取文件名
- 25. 如何获取文件名
- 26. 获取文件扩展名
- 27. 获取文件名C#
- 28. 获取文件夹名称
- 29. 获取文件名到myXhr.upload
- 30. 获取文件夹名称
在我有限的经验中,这比追踪.plist好一百万倍。 – buildsucceeded 2011-10-11 20:14:57