按書上的例子敲了敲:
#import <Foundation/Foundation.h>int main(int argc,const char *argv[]){ @autoreleasepool { //聲明檔案管理對象 NSFileManager *manager; //由defaultManager方法建立NSFileManager初始化 manager = [NSFileManager defaultManager]; //聲明要遍曆路徑 NSString *home; //初始化要遍曆的目錄:使用者根目錄 home = [@"~" stringByExpandingTildeInPath]; //聲明檔案遍曆對象 NSDirectoryEnumerator *direnum; //初始設定檔案遍曆對象,用檔案目錄對象home作參 direnum = [manager enumeratorAtPath:home]; //可變列表來儲存遍曆的檔案 NSMutableArray *files; //初始化大小設定為42 files = [NSMutableArray arrayWithCapacity:42]; //枚舉迭代的對象 NSString *filename; //使用迭代對象的方法來逐個遍曆枚舉中的所有元素 while(filename =[direnum nextObject]){ //判斷檔案是否是jpg格式的檔案 if([[filename pathExtension] isEqualTo:@"jpg"]){ //將檔案名稱添加到列表中 [files addObject:filename]; } } //聲明檔案清單枚舉 NSEnumerator *fileenum; //初始化枚舉對象 fileenum = [files objectEnumerator]; //枚舉遍曆所有元素 while (filename=[fileenum nextObject]) { //列印資訊 NSLog(@"%@",filename); } };//autoreleasepool return 0;}//main
輸出
2015-01-27 23:00:11.181 04.01 Shapes-Inheritance[449:9009] Desktop/psb.jpg2015-01-27 23:00:11.194 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/15.jpg2015-01-27 23:00:11.194 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/19.jpg2015-01-27 23:00:11.194 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/22.jpg2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/24.jpg2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/4.jpg2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/QPR.numbers/preview-micro.jpg2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/QPR.numbers/preview-web.jpg2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/QPR/QPR.numbers/preview.jpg2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/伯恩利/11.jpg2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/伯恩利/21.jpg2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/伯恩利/7.jpg2015-01-27 23:00:11.215 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/伯恩利/Burnley.numbers/preview-micro.jpg2015-01-27 23:00:11.215 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/伯恩利/Burnley.numbers/preview-web.jpg2015-01-27 23:00:11.215 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/伯恩利/Burnley.numbers/preview.jpg2015-01-27 23:00:11.216 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/10.jpg2015-01-27 23:00:11.216 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/14.jpg2015-01-27 23:00:11.216 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/19.jpg2015-01-27 23:00:11.217 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/2.jpg2015-01-27 23:00:11.217 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/21.jpg2015-01-27 23:00:11.217 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/4.jpg2015-01-27 23:00:11.218 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/7.jpg2015-01-27 23:00:11.218 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球員資訊/切爾西/chelsea.numbers/preview-micro.jpg