我有一个数据类型的变量 fileData,我正在努力寻找如何打印它的大小。
在过去的 NSData 中,您会打印长度,但无法使用此类型进行打印。
如何在 Swift 中打印数据的大小?
最佳答案
使用 yourData.count 并除以 1024 * 1024。使用 Alexanders 的好建议:
func stackOverflowAnswer() {
if let data = #imageLiteral(resourceName: "VanGogh.jpg").pngData() {
print("There were \(data.count) bytes")
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(data.count))
print("formatted result: \(string)")
}
}
结果如下:There were 28865563 bytes
formatted result: 28.9 MB
关于ios - 在 Swift 中打印数据的大小(兆字节),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42722498/