我正在尝试使用NSLog
打印我的NSData
对象的大小,但是目前我只能获取字节大小,方法如下:
NSLog(@"%u", myData.length);
那么我该如何修改这个
NSLog
语句,以便我可以看到类似于2.00 megs
的内容?任何帮助将不胜感激。NSLog(@"File size is : %.2f MB",(float)myData.length/1000.0f/1000.0f);
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