objective-c - 扩展至列表文件大小?

标签 objective-c cocoa

我刚刚学习 Objective-C/Cocoa,我有点不确定如何扩展我的代码以显示找到的文件的大小。

编辑2

我已采纳下面 SMorgan 和 Peter 的评论,并将其写入代码中……欢迎评论。

// ------------------------------------------------------------------- **
// 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;
}
// ------------------------------------------------------------------- **

干杯加里

最佳答案

您将使用-[NSFileManager fileAttributesAtPath:traverseLink:] (或者如果您仅定位 10.5+,则 -[NSFileManager attributesOfItemAtPath:error:] ),然后从返回的字典中读取 NSFileSize。

关于objective-c - 扩展至列表文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1487123/

相关文章:

objective-c - 如何推送 MFMailComposeViewController? (iOS 5)

objective-c - 比较两个表示日期的 NSString

macos - 在基于 View 的 NSTableView 中,NSTextField 被 NSImageView 裁剪

objective-c - Cocoa 框架和 C 静态库有什么区别?

xcode - Interface Builder 的新 IBDesignable 属性是否适用于 NSButton 和 Cocoa 中的其他控件?

objective-c - iOS6:MFMailComposeViewController 加载缓慢,闪黑屏; MailCompositionS 开始占用内存

iphone - 将数据 UITableViewCell 传递到 UITableViewCell

objective-c - 以编程方式更改 NSButton 状态和绑定(bind)值

cocoa - 有没有办法检测 NSToolbar 上的溢出

c++ - 如何将 std::cout 重定向到 UITextView?