字节 自 千字节
我正在检索文档目录中所有文件的大小。我正在使用方法 attributesOfItemAtPath
来执行此操作。它是成功的。但我得到的是字节形式和类 NSNumber
的输出。看起来不太好。
因此,我需要获取以 KB 或 MB 为单位的输出,并且必须将它们转换为 NSString 以便将其存储在 NSDictionary 中,因为我必须将其显示在表视图。请帮助我这样做。谢谢。
这是我的代码..
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 中的大小,但该代码不起作用。
cell.lblSize.text = (NSString *) [[directoryContent objectAtIndex:listIndex] objectForKey:@"filesSize"];
帮助我将文件的大小从字节转换为千字节并将其显示在表视图中。 先感谢您..
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.
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.
Here is my code..
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]];
}
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"];
Help me to convert the size of a file from byte to KiloByte and to display it in a tableView.
Thank you in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您愿意,可以使用我的 NSValueTransformer 子类:
You can use my NSValueTransformer subclass if you like:
四舍五入到最接近的 KB:
Rounded to the nearest KB:
考虑使用
NSNumber
而不是NSString
在NSDictionary
中存储数字。Consider using
NSNumber
instead ofNSString
to store numbers in anNSDictionary
.