Objective-c自学笔记(3)-数据集合的使用

时间:2021-09-25 19:53:57


按书上的例子敲了敲:


#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.jpg
2015-01-27 23:00:11.194 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/15.jpg
2015-01-27 23:00:11.194 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/19.jpg
2015-01-27 23:00:11.194 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/22.jpg
2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/24.jpg
2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/4.jpg
2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/QPR.numbers/preview-micro.jpg
2015-01-27 23:00:11.195 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/QPR.numbers/preview-web.jpg
2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/QPR/QPR.numbers/preview.jpg
2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/伯恩利/11.jpg
2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/伯恩利/21.jpg
2015-01-27 23:00:11.196 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/伯恩利/7.jpg
2015-01-27 23:00:11.215 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/伯恩利/Burnley.numbers/preview-micro.jpg
2015-01-27 23:00:11.215 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/伯恩利/Burnley.numbers/preview-web.jpg
2015-01-27 23:00:11.215 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/伯恩利/Burnley.numbers/preview.jpg
2015-01-27 23:00:11.216 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/10.jpg
2015-01-27 23:00:11.216 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/14.jpg
2015-01-27 23:00:11.216 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/19.jpg
2015-01-27 23:00:11.217 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/2.jpg
2015-01-27 23:00:11.217 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/21.jpg
2015-01-27 23:00:11.217 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/4.jpg
2015-01-27 23:00:11.218 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/7.jpg
2015-01-27 23:00:11.218 04.01 Shapes-Inheritance[449:9009] Documents/aodci/球员信息/切尔西/chelsea.numbers/preview-micro.jpg