Search code examples
objective-cnsfilemanager

NSFileManager filesize Incorrect for .xcodeproj File


From the Mac Finder app I know that my file PicViewer.xcodeproj has a files size of 35,697 bytes. The code below gives me a file size of 160. I know the code works as it correctly tells me the file size of an Excel file.

I used the code from this stackoverflow question How can i get my file size in Objective-C with a minor change.

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];
    NSString *filetype = [attributes fileType];
    NSLog(@"%@ %@ %llu", filename, filetype, size);
}

It also reports that the file type is NSFileTypeDirectory and not NSFileTypeRegular (and I suspect this is causing the file size to be reported incorrectly). For example:

Exercise Record.xlsx NSFileTypeRegular 637131

PicViewer.xcodeproj NSFileTypeDirectory 160

Any help would be greatly appreciated.


Solution

  • xcodeproj is a directory indeed, you can expand its contents in Finder using Show package content (or whatever, I do not use English MacOS) menu command, so in you want to calculate its size, you should do it recursively, for instance like that