Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将NSData长度从字节转换为兆字节_Ios_Nsdata_Nslog - Fatal编程技术网

Ios 将NSData长度从字节转换为兆字节

Ios 将NSData长度从字节转换为兆字节,ios,nsdata,nslog,Ios,Nsdata,Nslog,我正在尝试NSLog我的NSData对象的MEG数,但目前我只能使用 NSLog(@"%u", myData.length); 那么,我如何更改这个NSLog语句,以便看到类似的内容呢 2.00兆欧 任何帮助都将不胜感激。一个千字节中有1024个字节,一个兆字节中有1024个千字节,所以 NSLog(@"File size is : %.2f MB",(float)myData.length/1024.0f/1024.0f); 请注意,这是一种过于简单的方法,无法真正适应1048576字节以

我正在尝试
NSLog
我的
NSData
对象的MEG数,但目前我只能使用

NSLog(@"%u", myData.length);
那么,我如何更改这个
NSLog
语句,以便看到类似的内容呢

2.00兆欧


任何帮助都将不胜感激。

一个千字节中有1024个字节,一个兆字节中有1024个千字节,所以

NSLog(@"File size is : %.2f MB",(float)myData.length/1024.0f/1024.0f);
请注意,这是一种过于简单的方法,无法真正适应1048576字节以下或1073741823字节以上的字节大小。有关可处理不同文件大小的更完整解决方案,请参阅:

或适用于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种方法之一来解决问题


#1.使用字节计数格式化程序的类方法 以下示例代码显示了如何实现
字符串(fromByteCount:countStyle:)
,以便通过自动将字节转换为更合适的存储单元(例如兆字节)来打印文件大小:


#2.使用字节计数格式化程序的方法 以下示例代码显示了如何实现
字节计数格式化程序
字符串(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 displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)
print(displaySize) // prints: 2.6 MB
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
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
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
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

#3.使用
字节计数格式化程序
的类方法和
度量
以下示例代码显示了如何实现
字符串(from:countStyle:)
,以便通过自动将字节转换为更合适的存储单元(例如兆字节)来打印文件大小:


#4.使用字节计数格式化程序的方法和测量 以下示例代码显示了如何实现
字节计数格式化程序
字符串(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 displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)
print(displaySize) // prints: 2.6 MB
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
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
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
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

#5.使用
MeasurementFormatter
的方法和
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 displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)
print(displaySize) // prints: 2.6 MB
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
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
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
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

我该如何格式化它,使它后面有两个小数位。很酷,谢谢你的帮助。。我觉得我必须要有括号等等,这让我很困惑,但这很好地工作了!:)如果需要向用户显示,则不要使用字符串格式。相反,请使用NSNumberFormatter。这将为用户的locale.quick和hand设置格式。如何在没有MB的情况下将mega中的大小作为一个双倍值来获取很有帮助?@user1553381您可以将
ByteCountFormatter
的属性设置为
false
,以便只包含计数。