gpt4 book ai didi

ios - NSFileSystemFreeSize : translating result into user friendly display of mb/gb?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:19:53 25 4
gpt4 key购买 nike

有没有人有将 NSFileSystemFreeSize 的结果转换为用户友好的可用 mb/gb 字符串的例程。我以为我已经掌握了它的要点,但我得到了奇怪的结果。

- (NSString*)getFreeSpace
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSDictionary* fileAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:documentsDirectory error:NULL];
unsigned long long freeSpaceInBytes = [[fileAttributes objectForKey:NSFileSystemFreeSize] unsignedLongLongValue];

NSString * space = [NSString stringWithFormat:@"Free Space: %fll", freeSpaceInBytes /1024. / 1024. /1024.];

NSLog(@"freeSpaceInBytes %llull %fll", freeSpaceInBytes, freeSpaceInBytes /1024. / 1024. /1024.);

return space;
}

最佳答案

static NSString* prettyBytes(uint64_t numBytes)
{
uint64_t const scale = 1024;
char const * abbrevs[] = { "EB", "PB", "TB", "GB", "MB", "KB", "Bytes" };
size_t numAbbrevs = sizeof(abbrevs) / sizeof(abbrevs[0]);
uint64_t maximum = powl(scale, numAbbrevs-1);
for (size_t i = 0; i < numAbbrevs-1; ++i) {
if (numBytes > maximum) {
return [NSString stringWithFormat:@"%.4f %s", numBytes / (double)maximum, abbrevs[i]];
}
maximum /= scale;
}
return [NSString stringWithFormat:@"%u Bytes", (unsigned)numBytes];
}

关于ios - NSFileSystemFreeSize : translating result into user friendly display of mb/gb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10091816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com