本文介绍了字节到KiloBytes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检索文档目录中所有文件的大小。我使用方法 attributesOfItemAtPath 来执行此操作。它很成功。但我得到的输出是字节和类 NSNumber 。它看起来不太好。

I am retrieving the size of all files in the document directory. I am using the method attributesOfItemAtPath to do so. It is successful. But I am getting the output in the form of bytes and of class NSNumber. It does'nt look good.

所以,我需要以KB或MB的形式获取输出,我必须将它们转换为 NSString 以便将其存储在 NSDictionary 中,因为我必须在TableView中显示它。请帮我这样做。谢谢。

So, I need to get the output in KBs or MBs and I have to convert them into NSString in order to store that in a NSDictionary as I have to display it in a TableView. Please help me to do so. Thank you.

这是我的代码..

directoryContent = [[NSMutableArray alloc] init];
    for (NSString *path in paths){
filesDictionary  =[[NSMutableDictionary alloc] init];
filesSize = [[NSNumber alloc] init];
filesSize = [filesDictionary objectForKey:NSFileSize];
filesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:filesSize, @"filesSize", nil];
[directoryContent addObject:[filesDictionary copy]];
}

我使用以下代码绑定tableView中的大小工作。

And I am using the following code to bind the size in tableView which is not working.

cell.lblSize.text = (NSString *) [[directoryContent objectAtIndex:listIndex] objectForKey:@"filesSize"];

帮助我将文件大小从byte转换为KiloByte并将其显示在tableView中。
提前谢谢..

Help me to convert the size of a file from byte to KiloByte and to display it in a tableView.Thank you in advance..

推荐答案

舍入到最近的KB:

NSNumber *fileSize = [[directoryContent objectAtIndex:listIndex]
                      objectForKey:@"fileSize"];
cell.lblSize.text = [NSString stringWithFormat: @"%d",
                     (int)round([fileSize doubleValue] / 1024]);

这篇关于字节到KiloBytes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:29