Apple has changed how they calculate KB, MB, and GB in Max OS X 10.6. Instead of using 1024, they use 1000.
My question is how to deal with this in my code? I'm trying to get the amount of space free, so I get the number of bytes via NSFileManager
. When I go to display that to the user, I need to turn that into GBs differently depending on whether they're on 10.5 or 10.6.
Is there a built-in constant for GB size? (Or whatever it is you would call the 1024 number? ) It seemed a little silly to define my own.
## I'm currently doing something like this.
if (running10_6) {
double gbConst = 1000 * 1000 * 1000;
} else {
double gbConst = 1024 * 1024 * 1024;
}
NSDictionary *attr = [NSFM attributesOfFileSystemForPath:@"/" error:&error];
double bytes = [[attr objectForKey:NSFileSystemFreeSize] doubleValue];
double freeGB = bytes / gbConst;
Since OS X 10.8 / iOS 6.0, the new NSByteCountFormatter class does exactly what you want:
enum {
NSByteCountFormatterCountStyleFile = 0,
NSByteCountFormatterCountStyleMemory = 1,
NSByteCountFormatterCountStyleDecimal = 2,
NSByteCountFormatterCountStyleBinary = 3
};
Decimal/Binary are 1000 and 1024. File/Memory are aliases for whichever one the OS uses for that measure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With