简体   繁体   English

NSFileManager FileSize问题-可可OSX

[英]NSFileManager FileSize Problem - Cocoa OSX

I have a function that checks the size of several plist files in the /User/Library/Preferences/ directory. 我有一个功能可以检查/ User / Library / Preferences /目录中几个plist文件的大小。 For testing purposes, I'm using iTunes, which on my machine has a preference file of ~500kb. 为了进行测试,我使用的是iTunes,我的机器上的iTunes的首选项文件约为500kb。

EDIT: I have corrected my code as per the answer - as posted, this code works correctly. 编辑:我已经按照答案更正了我的代码-发布后,此代码正常工作。

    NSString *obj = @"iTunes";
    NSString *filePath = [NSString stringWithFormat:@"/Applications/%@.app",obj];
    NSString *bundle = [[NSBundle bundleWithPath:filePath] bundleIdentifier];

    NSString *PropertyList=[NSString stringWithFormat:@"/Preferences/%@.plist",bundle];
    NSString* fileLibraryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:PropertyList];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fileLibraryPath];

    if (fileExists) {
        NSError *err = nil;
        NSDictionary *fattrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileLibraryPath error:&err];

        if (fattrib != nil){            
            //Here I perform my comparisons

            NSLog(@"%i %@", [fattrib fileSize],obj);

        }
    }

However, no matter what I do, the size is returned as 102. Not 102kb, just 102. I have used objectForKey:NSFileSize, I have used stringValue, all 102. 但是,无论我做什么,大小都将返回为102。不是102kb,而是102。我使用了objectForKey:NSFileSize,我使用了stringValue,全都是102。


As stated in the selected answer below lesson learned is to always check the path you're submitting to NSFileManager. 如下面所选答案中所述,汲取的教训是,始终检查要提交给NSFileManager的路径。

Thanks! 谢谢!

The filePath that you are using in 您在其中使用的filePath

NSDictionary *fattrib = [ ... attributesOfItemAtPath:filePath error:&err];

appears to be 似乎

/Applications/iTunes.app

which on my system is a directory of size 102 bytes, same for /Applications/Mail.app - 102 bytes. 在我的系统上,该目录的大小为102字节,与/Applications/Mail.app相同-102字节。 Is it just that the path is not what you intend? 只是路径不是您想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM