// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rights reserved. // /* *读取指定目录下的文件列表 */ #import <Foundation/Foundation.h> void myQuickMethod(); int main(int argc, const char * argv[]) { //文件操作对象 NSFileManager *manager = [NSFileManager defaultManager]; //文件夹路径 NSString *home = [@"~" stringByExpandingTildeInPath];//根目录文件夹 //目录迭代器 NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:home]; //新建数组,存放各个文件路径 NSMutableArray *files = [NSMutableArray arrayWithCapacity:42]; //遍历目录迭代器,获取各个文件路径 NSString *filename; while (filename = [direnum nextObject]) { if ([[filename pathExtension] isEqualTo:@"jpg"]) {//筛选出文件后缀名是jpg的文件 [files addObject:filename]; } } NSLog(@"%lu",[files count]); //遍历数组,输出列表 NSEnumerator *enume = [files objectEnumerator]; while (filename = [enume nextObject]) { NSLog(@"%@",filename); } //myQuickMethod(); return 0; } //使用快速枚举来实现 void myQuickMethod(){ //文件操作对象 NSFileManager *manager = [NSFileManager defaultManager]; //所查找文件夹的路径 NSString *home = [@"~" stringByExpandingTildeInPath]; //目录迭代器 NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:home]; //快速枚举 NSMutableArray *files = [NSMutableArray arrayWithCapacity:42]; for (NSString *filename in direnum) { if ([[filename pathExtension] isEqualTo:@"jpg"]) { [files addObject:filename]; } } NSLog(@"%lu",[files count]); //快速枚举,输出结果 for (NSString *filename in files) { NSLog(@"%@",filename); } }
时间: 2024-10-24 22:41:00