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);
        }
相關文章
相關標籤/搜索