如何正确获取文件大小,并将其转换为MB,GB在Cocoa? [英] How to get file size properly and convert it to MB, GB in Cocoa?

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

问题描述


可能重复:

ObjC / 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 类。

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

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


NSByteCountFormatterCountStyleFile :指定文件或存储字节计数的显示。这个的实际行为是
平台特定;在OS X 10.8,这使用十进制风格,但
可能会随时间而变化。



NSByteCountFormatterCountStyleMemory :指定显示内存字节计数。它的实际行为是平台特定的;在OS
X 10.8,这使用二进制风格,但可能会随时间更改。



NSByteCountFormatterCountStyleDecimal :明确指定KB的字节数,1000字节显示为1 KB



NSByteCountFormatterCountStyleBinary :明确指定KB的字节数,1024字节显示为1 KB


现代的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]];
}


Possible Duplicate:
ObjC/Cocoa class for converting size to human-readable string?

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.

Example 5.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.

NSByteCountFormatterCountStyleFile: Specifies display of file or storage byte counts. The actual behavior for this is platform-specific; on OS X 10.8, this uses the decimal style, but that may change over time.

NSByteCountFormatterCountStyleMemory: Specifies display of memory byte counts. The actual behavior for this is platform-specific; on OS X 10.8, this uses the binary style, but that may change over time.

NSByteCountFormatterCountStyleDecimal: Specifies the number of bytes for KB explicitly, 1000 bytes are shown as 1 KB

NSByteCountFormatterCountStyleBinary: Specifies the number of bytes for KB explicitly, 1024 bytes are shown as 1 KB

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆