Obj-C集合使用demo

       //遍历~目录下,后缀名为jpg的文件的,然后打印"路径/文件"
        NSFileManager *manger;
        manger = [NSFileManager defaultManager]; //singleton instance
        
        NSString *home;                           //home path
        home = [@"~" stringByExpandingTildeInPath]; //use home path instead of ~ by the method
        
        NSDirectoryEnumerator *direnum;
        direnum = [manger enumeratorAtPath:home]; //all of the file path
        
        NSMutableArray *files;
        files = [NSMutableArray arrayWithCapacity:42];
        
        NSString *filename;
        while (filename = [direnum nextObject]) {
            if ([[filename pathExtension] isEqualToString:@"jpg"]) { //pathExtension: file extension
                [files addObject:filename];
            }
        }
        
        NSEnumerator *fileenum;
        fileenum = [files objectEnumerator]; //迭代器,类似于C++中的iterator
        
        while (filename = [fileenum nextObject]) {
            NSLog(@"%@", filename);
        }



你可能感兴趣的:(Obj-C集合使用demo)