ios开发系列之使用NSDirectoryEnumerator的对象获得目录的资源列表
2015-10-27 17:28
821 查看
NSDirectoryEnumerator,目录枚举类,一个
NSDirectoryEnumerator对象列举了一个目录的内容,返回的所有文件在目录中的路径名,在该目录中。这些路径是相对于目录而言的。
enumeratorAtPath:这个方法一次可以枚举指定目录中的每一个文件. 默认情况下,如果其中一个文件为包含子文件,那么也会递归的枚举它的子文件. 该方法返回一个目录的所有资源列表.
下面以app所在路径为例取得以
mp3为后缀的文件名和文件路径
// 类
NSBundle的对象是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,
nib文件(用户也会把
bundle称为
plug-in).
NSFileManager * fileManager = [NSFileManager defaultManager]; NSDirectoryEnumerator * myDirectoryEnumerator = [fileManager enumeratorAtPath:[NSBundle mainBundle].bundlePath]; NSMutableArray * filePathArray = [[NSMutableArray alloc]init];//存放文件路径数组 NSMutableArray * fileNameArray = [[NSMutableArray alloc]init];//存放文件名数组 NSString * file;//声明文件名 while (file = [myDirectoryEnumerator nextObject]) { if ([file.pathExtension isEqualToString:@"mp3"]) { //判断file的后缀名是否为mp3 [filePathArray addObject:[self bundlePath:file]]; [fileNameArray addObject:file]; } } - (NSString *)bundlePath:(NSString *)fileName{ return [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:fileName]; }
nextObject方法返回集合中下一个枚举对象, 当所有对象都被枚举完了,则返回
nil;
NSArray *anArray = // ... ; NSEnumerator *enumerator = [anArray objectEnumerator]; id object; while ((object = [enumerator nextObject])) { // do something with object... }
相关文章推荐
- iOS开发实用技术之真机调试、内购/广告
- iOS 3D Touch 适配开发
- iOS开发一道亮丽的风景线
- iOS 简易无限滚动的图片轮播器-SDCycleScrollView
- iOS 简易无限滚动的图片轮播器-SDCycleScrollView
- iOS开发new与alloc/init的区别
- iOS 百度地图反编码地理位置信息
- 牛逼的ios在github上的库
- iOS—dictionary写入文件出现的几个问题
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版
- iOS 国际化多语言设置 xcode7
- 第三方分享遇到的特殊问题
- IOS--引导页面
- iOS开发的未来
- lldb调试到ios的main函数
- CGContextSaveGState与CGContextRestoreGState的作用 (转)
- Nagios监控Windows服务器
- iOS图片的异步加载
- library not found for IPods/xxxx库 解决办法
- iOS单例解析