38

我正在尝试NSLog我的对象的兆数,NSData但是目前我所能得到的只是字节数

NSLog(@"%u", myData.length);

那么我将如何更改此NSLog语句以便我可以看到类似

2.00 兆

任何帮助,将不胜感激。

4

3 回答 3

124

一千字节有 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)))
于 2012-12-10T22:21:35.293 回答
16

对于 Swift 3,以 Mb 为单位:

let countBytes = ByteCountFormatter()
countBytes.allowedUnits = [.useMB]
countBytes.countStyle = .file
let fileSize = countBytes.string(fromByteCount: Int64(dataToMeasure!.count))

print("File size: \(fileSize)")
于 2017-04-25T20:17:39.430 回答
4

在 Swift 5.1 和 iOS 13 中,您可以使用以下 5 种方法之一来解决您的问题。


#1。使用ByteCountFormatterstring(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

#2。使用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

#3。使用ByteCountFormatterstring(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

#4。使用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

#5。使用MeasurementFormatter'sstring(from:)方法和Measurement

以下示例代码显示了如何实现MeasurementMeasurementFormatterstring(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
于 2019-06-24T18:19:46.813 回答