我有一个函数可以检查/User/Library/Preferences/目录中几个plist文件的大小。出于测试目的,我使用的是iTunes,它在我的机器上有一个大约500‘m的首选文件。
编辑:我已经根据答案更正了我的代码--正如贴出的,这段代码工作正常。
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。不是102 102,只是102 102。我使用了objectForKey:NSFileSize,我使用了stringValue,全部为102个。
如下面所选答案所述,经验教训是始终检查要提交给NSFileManager的路径。
谢谢!
发布于 2011-01-19 21:34:39
您正在使用的filePath
NSDictionary *fattrib = [ ... attributesOfItemAtPath:filePath error:&err];似乎是
/Applications/iTunes.app在我的系统中是一个大小为102字节的目录,/Applications/Mail.app - 102字节的目录也是如此。难道这条路不是你想要的吗?
https://stackoverflow.com/questions/4740897
复制相似问题