我正在尝试NSLog
我的对象的兆数,NSData
但是目前我所能得到的只是字节数
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 类用于将大小转换为人类可读的字符串?
或者对于 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
'sstring(fromByteCount:)
方法以下示例代码显示了如何实现ByteCountFormatter
'sstring(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
'sstring(from:)
方法和Measurement
以下示例代码显示了如何实现ByteCountFormatter
'sstring(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
'sstring(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