本文介绍了如何正确获取文件大小,并将其转换为MB,GB在Cocoa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cocoa的新人。我正在尝试正确获取文件夹文件的大小。如果它少于1 GB或以GB为单位,则以MB为单位显示。



我想显示的方式在点后四舍五入。



示例
5.5 MB (如果超过1000) 1.1 GB



我尝试使用此

  unsigned long long size =([[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil] fileSize]); 

但我不能正确转换数字,并显示它, p>

感谢。

解决方案

函数

   - (id)transformedValue:(id)value 
{

double convertedValue = [value doubleValue];
int multiplyFactor = 0;

NSArray * tokens = [NSArray arrayWithObjects:@bytes,@KB,@MB,@GB,@TB,@PB ,ZB,YB,nil];

while(convertedValue> 1024){
convertedValue / = 1024;
multiplyFactor ++;
}

return [NSString stringWithFormat:@%4.2f%@,convertedValue,[tokens objectAtIndex:multiplyFactor]];
}

编辑:



您还可以使用类。

  [NSByteCountFormatter stringFromByteCount:1999 countStyle:NSByteCountFormatterCountStyleFile];可在iOS 6.0 / OS X v10.8及更高版本中使用。 

您可以使用 NSByteCountFormatterCountStyleFile NSByteCountFormatterCountStyleMemory NSByteCountFormatterCountStyleDecimal NSByteCountFormatterCountStyleBinary in countStyle。

现代的Objective-c syntext:

   - (id)transformedValue:(id)value 
{

double convertedValue = [value doubleValue];
int multiplyFactor = 0;

NSArray * tokens = @ [@bytes,@KB,@MB,@GB,@TB,@PB,@EB ZB,YB];

while(convertedValue> 1024){
convertedValue / = 1024;
multiplyFactor ++;
}

return [NSString stringWithFormat:@%4.2f%@,convertedValue,tokens [multiplyFactor]];
}


I'm new in Cocoa. I'm trying to get size of folder files properly. And display it in MB if it less 1 GB , or in GB.

The way I want it to display is rounded with one number after point.

Example5.5 MB if it is more than 1000 > 1.1 GB

I'm trying to use this

 unsigned  long long size= ([[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil] fileSize]);

But I can't a way properly convert number, and display it , as I want.

Thanks.

解决方案

For converting file size to MB, Gb use below function

- (id)transformedValue:(id)value
{

    double convertedValue = [value doubleValue];
    int multiplyFactor = 0;

    NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB", @"ZB", @"YB",nil];

    while (convertedValue > 1024) {
        convertedValue /= 1024;
        multiplyFactor++;
    }

    return [NSString stringWithFormat:@"%4.2f %@",convertedValue, [tokens objectAtIndex:multiplyFactor]];
}

EDIT:

You can also use NSByteCountFormatter class. Available in iOS 6.0 / OS X v10.8 and later.

[NSByteCountFormatter stringFromByteCount:1999 countStyle:NSByteCountFormatterCountStyleFile];

You can use NSByteCountFormatterCountStyleFile, NSByteCountFormatterCountStyleMemory, NSByteCountFormatterCountStyleDecimal or NSByteCountFormatterCountStyleBinary in countStyle.

Modern Objective-c syntext:

- (id)transformedValue:(id)value
{

    double convertedValue = [value doubleValue];
    int multiplyFactor = 0;

    NSArray *tokens = @[@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB", @"ZB", @"YB"];

    while (convertedValue > 1024) {
        convertedValue /= 1024;
        multiplyFactor++;
    }

    return [NSString stringWithFormat:@"%4.2f %@",convertedValue, tokens[multiplyFactor]];
}

这篇关于如何正确获取文件大小,并将其转换为MB,GB在Cocoa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 18:56