开发者

NSFileManager FileSize Problem - Cocoa OSX

开发者 https://www.devze.com 2023-02-05 08:42 出处:网络
I have a function that checks the size of several plist files in the /User/Library/Preferences/ directory. For testing purposes, I\'m using iTunes, which on my machine has a preference file of ~500kb.

I have a function that checks the size of several plist files in the /User/Library/Preferences/ directory. For testing purposes, I'm using iTunes, which on my machine has a preference file of ~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.


As stated in the selected answer below lesson learned is to always check the path you're submitting to NSFileManager.

Thanks!


The filePath that you are using in

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. Is it just that the path is not what you intend?

0

精彩评论

暂无评论...
验证码 换一张
取 消