在 Swift 中打印数据的大小(兆字节)
Print the size (megabytes) of Data in Swift
我有一个数据类型的变量 fileData,我正在努力寻找如何打印它的大小。
在过去的 NSData 中,您会打印长度,但使用此类型无法做到这一点。
如何在Swift中打印数据的大小?
您可以使用 count
的 Data 对象,仍然可以使用 length
的 NSData
count 应该适合您的需要。您需要将字节转换为兆字节 (Double(data.count) / pow(1024, 2)
)
使用 yourData.count 并除以 1024 * 1024。使用 Alexanders 极好的建议:
func WhosebugAnswer() {
if let data = #imageLiteral(resourceName: "VanGogh.jpg").pngData() {
print("There were \(data.count) bytes")
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(data.count))
print("formatted result: \(string)")
}
}
结果如下:
There were 28865563 bytes
formatted result: 28.9 MB
如果您的目标是打印尺寸以供使用,请使用 ByteCountFormatter
import Foundation
let byteCount = 512_000 // replace with data.count
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(byteCount))
print(string)
根据已接受的答案,我创建了简单的扩展:
extension Data {
func sizeString(units: ByteCountFormatter.Units = [.useAll], countStyle: ByteCountFormatter.CountStyle = .file) -> String {
let bcf = ByteCountFormatter()
bcf.allowedUnits = units
bcf.countStyle = .file
return bcf.string(fromByteCount: Int64(count))
}}
如果您只想查看字节数,直接打印数据对象即可。
let dataObject = Data()
print("Size is \(dataObject)")
应该给你:
Size is 0 bytes
换句话说,.count
在较新的 Swift 3.2 或更高版本中将不是必需的。
在以下代码中输入您的文件 URL 以获取以 MB 为单位的文件大小,希望这对您有所帮助。
let data = NSData(contentsOf: FILE URL)!
let fileSize = Double(data.count / 1048576) //Convert in to MB
print("File size in MB: ", fileSize)
获取字符串的大小,改编自
if let data = "some string".data(using: .utf8)! {
print("There were \(data.count) bytes")
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useKB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(data.count))
print("formatted result: \(string)")
}
Swift 5.1
extension Int {
var byteSize: String {
return ByteCountFormatter().string(fromByteCount: Int64(self))
}
}
用法:
let yourData = Data()
print(yourData.count.byteSize)
获取 Data
大小(以兆字节为单位)的快速扩展,如 Double
。
extension Data {
func getSizeInMB() -> Double {
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB]
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(self.count)).replacingOccurrences(of: ",", with: ".")
if let double = Double(string.replacingOccurrences(of: " MB", with: "")) {
return double
}
return 0.0
}
}
我有一个数据类型的变量 fileData,我正在努力寻找如何打印它的大小。
在过去的 NSData 中,您会打印长度,但使用此类型无法做到这一点。
如何在Swift中打印数据的大小?
您可以使用 count
的 Data 对象,仍然可以使用 length
的 NSData
count 应该适合您的需要。您需要将字节转换为兆字节 (Double(data.count) / pow(1024, 2)
)
使用 yourData.count 并除以 1024 * 1024。使用 Alexanders 极好的建议:
func WhosebugAnswer() {
if let data = #imageLiteral(resourceName: "VanGogh.jpg").pngData() {
print("There were \(data.count) bytes")
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(data.count))
print("formatted result: \(string)")
}
}
结果如下:
There were 28865563 bytes
formatted result: 28.9 MB
如果您的目标是打印尺寸以供使用,请使用 ByteCountFormatter
import Foundation
let byteCount = 512_000 // replace with data.count
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(byteCount))
print(string)
根据已接受的答案,我创建了简单的扩展:
extension Data {
func sizeString(units: ByteCountFormatter.Units = [.useAll], countStyle: ByteCountFormatter.CountStyle = .file) -> String {
let bcf = ByteCountFormatter()
bcf.allowedUnits = units
bcf.countStyle = .file
return bcf.string(fromByteCount: Int64(count))
}}
如果您只想查看字节数,直接打印数据对象即可。
let dataObject = Data()
print("Size is \(dataObject)")
应该给你:
Size is 0 bytes
换句话说,.count
在较新的 Swift 3.2 或更高版本中将不是必需的。
在以下代码中输入您的文件 URL 以获取以 MB 为单位的文件大小,希望这对您有所帮助。
let data = NSData(contentsOf: FILE URL)!
let fileSize = Double(data.count / 1048576) //Convert in to MB
print("File size in MB: ", fileSize)
获取字符串的大小,改编自
if let data = "some string".data(using: .utf8)! {
print("There were \(data.count) bytes")
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useKB] // optional: restricts the units to MB only
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(data.count))
print("formatted result: \(string)")
}
Swift 5.1
extension Int {
var byteSize: String {
return ByteCountFormatter().string(fromByteCount: Int64(self))
}
}
用法:
let yourData = Data()
print(yourData.count.byteSize)
获取 Data
大小(以兆字节为单位)的快速扩展,如 Double
。
extension Data {
func getSizeInMB() -> Double {
let bcf = ByteCountFormatter()
bcf.allowedUnits = [.useMB]
bcf.countStyle = .file
let string = bcf.string(fromByteCount: Int64(self.count)).replacingOccurrences(of: ",", with: ".")
if let double = Double(string.replacingOccurrences(of: " MB", with: "")) {
return double
}
return 0.0
}
}