簡體   English   中英

NSFileManager FileSize問題-可可OSX

[英]NSFileManager FileSize Problem - Cocoa OSX

我有一個功能可以檢查/ User / Library / Preferences /目錄中幾個plist文件的大小。 為了進行測試,我使用的是iTunes,我的機器上的iTunes的首選項文件約為500kb。

編輯:我已經按照答案更正了我的代碼-發布后,此代碼正常工作。

    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);

        }
    }

但是,無論我做什么,大小都將返回為102。不是102kb,而是102。我使用了objectForKey:NSFileSize,我使用了stringValue,全都是102。


如下面所選答案中所述,汲取的教訓是,始終檢查要提交給NSFileManager的路徑。

謝謝!

您在其中使用的filePath

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

似乎

/Applications/iTunes.app

在我的系統上,該目錄的大小為102字節,與/Applications/Mail.app相同-102字節。 只是路徑不是您想要的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM