5

我的问题:

NSString *path = @"~/Desktop/folder/";
pathg = [path stringByExpandTitlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *array = [fm contentsOfDirectoryAtPath: path error: NULL];

for(int i = 0; i < array.count; ++i)
{
    NSFileSize *num = [array[i] fileSize];
}

错误:使用未声明的标识符“num”

4

2 回答 2

12

如果需要获取单个文件的文件大小,可以通过构造文件 URL 并直接查询 URL 的属性来实现。

NSString *filePath = [@"~/.bash_history" stringByExpandingTildeInPath];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSNumber *fileSizeValue = nil;
NSError *fileSizeError = nil;
[fileURL getResourceValue:&fileSizeValue
                   forKey:NSURLFileSizeKey
                    error:&fileSizeError];
if (fileSizeValue) {
    NSLog(@"value for %@ is %@", fileURL, fileSizeValue);
}
else {
    NSLog(@"error getting size for url %@ error was %@", fileURL, fileSizeError);
}

如果您需要迭代目录的内容,就像您的问题一样,您可以执行基于 URL 的版本以及问题中的方式。UsingcontentsOfDirectoryAtPath:...将文件名数组返回为NSStrings,并且您必须重新构建完整路径并获取属性作为附加步骤:

    NSString *directoryPath = [@"~" stringByExpandingTildeInPath];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *array = [fm contentsOfDirectoryAtPath: directoryPath error:NULL];
    for (NSString *filename in array) {
        NSError *attributesError = nil;
        NSDictionary *attributes = [fm attributesOfItemAtPath:[directoryPath stringByAppendingPathComponent:filename]
                                                        error:&attributesError];
        unsigned long long size = [attributes fileSize]; //Note this isn't a pointer
        NSLog(@"%llu", size);
    }

您可以使用文件 URL 制作类似的解决方案:

    NSString *directoryPath = [@"~" stringByExpandingTildeInPath];
    NSURL *directoryURL = [NSURL fileURLWithPath:directoryPath
                                     isDirectory:YES];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *contentsError = nil;
    NSArray *contents = [fm contentsOfDirectoryAtURL:directoryURL
                          includingPropertiesForKeys:@[NSURLFileSizeKey, NSURLIsDirectoryKey]
                                             options:0
                                               error:&contentsError];
    if (contents) {
        for (NSURL *contentURL in contents) {
            NSError *isDirectoryError = nil;
            NSNumber *isDirectoryNumber = nil;
            [contentURL getResourceValue:&isDirectoryNumber
                                  forKey:NSURLIsDirectoryKey
                                   error:&isDirectoryError];
            if (isDirectoryNumber) {
                if (![isDirectoryNumber boolValue]) {
                    NSNumber *fileSizeNumber = nil;
                    NSError *sizeError = nil;
                    [contentURL getResourceValue:&fileSizeNumber
                                          forKey:NSURLFileSizeKey
                                           error:&sizeError];
                    if (fileSizeNumber) {
                        NSInteger size = [fileSizeNumber integerValue];
                        NSLog(@"%li", (long)size);
                    }
                    else {
                        NSLog(@"error getting file size for file %@ error:%@",contentURL,sizeError);
                    }
                }
            }
            else {
                NSLog(@"error getting is url %@ was directory: %@", contentURL, isDirectoryError);
            }
        }
    }
    else {
        NSLog(@"error getting contents for directory %@ error: %@", directoryURL, contentsError);
    }
于 2013-04-01T12:09:46.893 回答
3

试试这个:

NSString *yourPath=[@"~/Desktop/folder/abc.png" stringByExpandingTildeInPath];
NSFileManager *fileManager=[NSFileManager defaultManager];
NSDictionary *attributesDict=[fileManager attributesOfItemAtPath:yourPath error:NULL];
NSInteger fileSize=[attributesDict fileSize];

NSLog(@"%ld",fileSize);
于 2013-04-01T12:10:02.573 回答