问题
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