我只是在学习Objective-C / Cocoa,我有点不确定如何扩展我的代码以显示找到的文件的大小。
我接受了SMorgan&的评论。彼得在下面,并将他们纳入代码...评论最受欢迎。
// ------------------------------------------------------------------- **
// DISC: FILEWALKER ..... (cocoa_fileWalker.m)
// DATE: 28th September 2009
// DESC: List all "*.exr" files in specified directory
// ------------------------------------------------------------------- **
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *fileName;
NSDictionary *attrDir;
NSError *myError;
NSNumber *fileSize;
NSFileManager *manager = [NSFileManager defaultManager];
NSString *rootPath = [@"~/Pictures" stringByExpandingTildeInPath];
NSMutableArray *fileArray = [[NSMutableArray alloc] init];
NSMutableString *newPath = [[NSMutableString alloc] init];
NSLog(@"Home: %@",rootPath);
for(fileName in [manager enumeratorAtPath:rootPath]){
if ([[fileName pathExtension] isEqual:@"exr"]) {
[fileArray addObject:fileName];
[newPath setString:rootPath];
[newPath appendString:@"/"];
[newPath appendString:fileName];
attrDir = [manager attributesOfItemAtPath:newPath error:&myError];
fileSize = [attrDir objectForKey: @"NSFileSize"];
NSLog(@"File: /%@ Size: %@", fileName, fileSize);
}
}
[fileArray release];
[newPath release];
[pool drain];
return 0;
}
// ------------------------------------------------------------------- **
欢呼加里
答案 0 :(得分:2)
您可以使用-[NSFileManager fileAttributesAtPath:traverseLink:](或者如果您只定位10.5 +,-[NSFileManager attributesOfItemAtPath:error:]),然后从返回的字典中读取NSFileSize。