NSFileSystemFreeSize

╄→尐↘猪︶ㄣ 提交于 2020-01-14 06:08:04

问题


I get a negaitve number from trying to use this function can anyone help. see code below

NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:dbPath error:Error];
NSDictionary *fileSysAttributes = [fileManager fileSystemAttributesAtPath:dbPath];

NSNumber *FileSize = [fileAttributes objectForKey:NSFileSize];
NSNumber *FreeSpace = [fileSysAttributes objectForKey:NSFileSystemFreeSize];


NSLog(@"FileSystem = %@",fileSysAttributes); // gives good values

NSLog(@"File Size = %d", [FileSize longLongValue]);  // gives good values

NSLog(@"System Space = %d",[FreeSpace longLongValue]);  //shows -ve for 45GB space

long long Result = FreeSpace - FileSize;

NSLog(@"Result = %d",Result);

The first and second log statements give good results but the third shows a negative number when I'm trying to use the longLongValue of FreeSpace directly


回答1:


Two problems:

1) You should be using "%lld" in your NSLog format strings http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Strings/Articles/formatSpecifiers.html

2) This code:

long long Result = FreeSpace - FileSize;

Is subtracting the addresses of the two NSNumber objects, not their values!

I think you mean:

long long Result = [FreeSpace longLongValue] - [FileSize longLongValue];


来源:https://stackoverflow.com/questions/2319437/nsfilesystemfreesize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!