gpt4 book ai didi

objective-c - NSFileManager 文件大小问题 - Cocoa OSX

转载 作者:行者123 更新时间:2023-12-03 16:59:42 25 4
gpt4 key购买 nike

我有一个函数可以检查/User/Library/Preferences/目录中多个 plist 文件的大小。出于测试目的,我使用 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 字节。难道只是这条路不是你想要的吗?

关于objective-c - NSFileManager 文件大小问题 - Cocoa OSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4740897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com