迭代目錄時調用fileAttributes方法的代價是多少?NSDirectoryEnumerator'fileAttributes方法的性能特點是什麼?
特別是,我想檢查一個路徑被枚舉是一個目錄。使用fileAttributes字典或文件管理器更好嗎? (下面的例子)
NSString *path = "/User/Jack/Documents";
NSDirectoryEnumerator *dirEnum = [filemanager enumeratorAtPath:path];
NSString *file;
BOOL isDir;
while (file = [dirEnum nextObject]) {
NSLog(@"File: %@", file);
NSString * p = [path stringByAppendingPathComponent:file];
BOOL isDir1;
[[NSFileManager defaultManager] fileExistsAtPath:p isDirectory:&isDir1];
BOOL isDir2 = [[dirEnum fileAttributes] objectForKey:@"NSFileType"] == NSFileTypeDirectory;
NSLog(@"isDirectory using fileExistsAtPath:isDirectory: = %d", isDir1);
NSLog(@"isDirectory using FileAttributes = %d", isDir2);
}
fileAttributes
返回一噸的除「不必要的」信息只是isDirectory,這裏有一個樣本輸出
2011-12-23 16:17:40.523 App[10190:707] File Attributes: {
NSFileCreationDate = "2011-10-23 04:04:51 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 80;
NSFileGroupOwnerAccountName = admin;
NSFileModificationDate = "2011-10-23 04:07:52 +0000";
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = Tony;
NSFilePosixPermissions = 493;
NSFileReferenceCount = 6;
NSFileSize = 204;
NSFileSystemFileNumber = 8381694;
NSFileSystemNumber = 234881029;
NSFileType = NSFileTypeDirectory;
}
,而調用文件管理器似乎浪費,以及因爲我們已經列舉有問題的目錄。
任何見解?