【发布时间】:2012-12-10 22:15:22
【问题描述】:
我正在尝试 NSLog 我的 NSData 对象的 meg 数,但是目前我只能通过使用获得字节数
NSLog(@"%u", myData.length);
那么我将如何更改此 NSLog 语句,以便我可以看到类似
2.00 兆
任何帮助将不胜感激。
【问题讨论】:
-
我将如何格式化它以使其后显示两位小数。
我正在尝试 NSLog 我的 NSData 对象的 meg 数,但是目前我只能通过使用获得字节数
NSLog(@"%u", myData.length);
那么我将如何更改此 NSLog 语句,以便我可以看到类似
2.00 兆
任何帮助将不胜感激。
【问题讨论】:
一千字节有 1024 字节,一兆字节有 1024 千字节,所以...
NSLog(@"File size is : %.2f MB",(float)myData.length/1024.0f/1024.0f);
请注意,这是一种简单的方法,无法真正适应低于 1,048,576 字节或高于 1,073,741,823 字节的字节大小。有关可以处理不同文件大小的更完整的解决方案,请参阅:ObjC/Cocoa class for converting size to human-readable string?
或者对于 OS X 10.8+ 和 iOS 6+
NSLog(@"%@", [[NSByteCountFormatter new] stringFromByteCount:data.length]);
在斯威夫特中:
print(ByteCountFormatter().string(fromByteCount: Int64(data.count)))
【讨论】:
对于 Swift 3,以 Mb 为单位:
let countBytes = ByteCountFormatter()
countBytes.allowedUnits = [.useMB]
countBytes.countStyle = .file
let fileSize = countBytes.string(fromByteCount: Int64(dataToMeasure!.count))
print("File size: \(fileSize)")
【讨论】:
在 Swift 5.1 和 iOS 13 中,您可以使用以下 5 种方法之一来解决您的问题。
ByteCountFormatter的string(fromByteCount:countStyle:)类方法以下示例代码展示了如何实现string(fromByteCount:countStyle:),以便通过自动将字节转换为更合适的存储单位(例如兆字节)来打印文件大小:
import Foundation
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)
let byteCount = data.count
print(byteCount) // prints: 2636725
let displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)
print(displaySize) // prints: 2.6 MB
ByteCountFormatter的string(fromByteCount:)方法以下示例代码展示了如何实现ByteCountFormatter的string(fromByteCount:),以便通过手动将字节转换为兆字节来打印文件大小:
import Foundation
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)
let byteCount = data.count
print(byteCount) // prints: 2636725
let formatter = ByteCountFormatter()
formatter.allowedUnits = [.useMB]
formatter.countStyle = .file
let displaySize = formatter.string(fromByteCount: Int64(byteCount))
print(displaySize) // prints: 2.6 MB
ByteCountFormatter 的string(from:countStyle:) 类方法和Measurement
以下示例代码展示了如何实现string(from:countStyle:),以便通过自动将字节转换为更合适的存储单位(例如兆字节)来打印文件大小:
import Foundation
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)
let byteCount = data.count
print(byteCount) // prints: 2636725
let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
let displaySize = ByteCountFormatter.string(from: byteSize, countStyle: .file)
print(displaySize) // prints: 2.6 MB
ByteCountFormatter 的string(from:) 方法和Measurement
以下示例代码显示了如何实现ByteCountFormatter 的string(from:) 和Measurement,以便通过手动将字节转换为兆字节来打印文件大小:
import Foundation
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)
let byteCount = data.count
print(byteCount) // prints: 2636725
let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
let formatter = ByteCountFormatter()
formatter.allowedUnits = [.useMB]
formatter.countStyle = .file
let displaySize = formatter.string(from: byteSize)
print(displaySize) // prints: 2.6 MB
MeasurementFormatter 的string(from:) 方法和Measurement
以下示例代码展示了如何实现Measurement 和MeasurementFormatter 的string(from:),以便通过手动将字节转换为兆字节来打印文件大小:
import Foundation
let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let data = try! Data(contentsOf: url)
let byteCount = data.count
print(byteCount) // prints: 2636725
let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
let convertedSize = byteSize.converted(to: .megabytes)
let formatter = MeasurementFormatter()
let displaySize = formatter.string(from: convertedSize)
print(displaySize) // prints: 2.637 MB
【讨论】:
ByteCountFormatter 的includesUnit 属性设置为false,以便仅包含计数。