深入探索文件路径操作与基本文件处理
1. 目录枚举
在进行目录枚举时,可向文件管理器对象发送 enumerationAtPath: 消息。例如:
NSFileManager *fm = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:path];
NSString *path;
while ((path = [dirEnum nextObject]) != nil) {
NSLog(@"%@", path);
}
此方法会返回一个 NSDirectoryEnumerator 对象,每次向该对象发送 nextObject 消息,就能获取目录中下一个文件的路径。当没有更多文件可枚举时,会返回 nil 。
enumeratorAtPath: 与 directoryContentsAtPath: 方法有所不同。 enumeratorAtPath: 会列出指定目录及其子目录的内容,而 directoryContentsAtPath: 不会。若要在枚举时阻止子目录的枚举,可按如下方式修改代码:
BOOL flag;
while ((path = [dirE
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



