首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展到list FileSize?

扩展到list FileSize?
EN

Stack Overflow用户
提问于 2009-09-28 13:43:23
回答 1查看 121关注 0票数 0

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

编辑2

我已经从下面的SMorgan & Peter那里获取了注释,并将它们写入代码中……欢迎发表评论。

代码语言:javascript
复制
// ------------------------------------------------------------------- **
// 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;
}
// ------------------------------------------------------------------- **

干杯加里

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-09-28 14:20:46

您可以使用-[NSFileManager fileAttributesAtPath:traverseLink:] (或者如果您只针对10.5+、-[NSFileManager attributesOfItemAtPath:error:]),然后从返回的字典中读取NSFileSize。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1487123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档