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クラス?
または、OSX10.8以降およびiOS6以降の場合
NSLog(@"%@", [[NSByteCountFormatter new] stringFromByteCount:data.length]);
Swiftの場合:
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)")
Swift5.1とiOS13では、次の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