字节 自 千字节

发布于 2024-10-02 22:45:50 字数 872 浏览 2 评论 0原文

我正在检索文档目录中所有文件的大小。我正在使用方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄暮涼年 2024-10-09 22:45:50

如果您愿意,可以使用我的 NSValueTransformer 子类:

@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

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
信愁 2024-10-09 22:45:50

四舍五入到最接近的 KB:

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

Rounded to the nearest KB:

NSNumber *fileSize = [[directoryContent objectAtIndex:listIndex]
                      objectForKey:@"fileSize"];
cell.lblSize.text = [NSString stringWithFormat: @"%d",
                     (int)round([fileSize doubleValue] / 1024]);
久而酒知 2024-10-09 22:45:50

考虑使用 NSNumber 而不是 NSStringNSDictionary 中存储数字。

Consider using NSNumber instead of NSString to store numbers in an NSDictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文