I\'m trying to figure out how to convert a file\'s (or directory\'s) byte size into kilobytes, megabytes, gigabytes, etc... respectively according the file\'s or directory\'s si
You can use my NSValueTransformer
subclass if you like:
@interface FileSizeTransformer : NSValueTransformer {
}
+ (Class)transformedValueClass;
+ (BOOL)allowsReverseTransformation;
- (id)transformedValue:(id)value;
@end
@implementation FileSizeTransformer
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
- (id)transformedValue:(id)value;
{
if (![value isKindOfClass:[NSNumber class]])
return nil;
double convertedValue = [value doubleValue];
int multiplyFactor = 0;
NSArray *tokens = [NSArray arrayWithObjects:@"B",@"KB",@"MB",@"GB",@"TB",nil];
while (convertedValue > 1024) {
convertedValue /= 1024;
multiplyFactor++;
}
return [NSString stringWithFormat:@"%4.2f %@",convertedValue, [tokens objectAtIndex:multiplyFactor],value];
}
@end