ios 在 Swift 中打印数据的大小(兆字节)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42722498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:59:27  来源:igfitidea点击:

Print the size (megabytes) of Data in Swift

iosswiftcocoa-touchnsdatafoundation

提问by user2512523

I have a variable fileData of Data type and I am struggling to find how to print the size of this.

我有一个数据类型的变量 fileData,我正在努力寻找如何打印它的大小。

In the past NSData you would print the length but unable to do that with this type.

在过去的 NSData 中,您会打印长度,但无法使用此类型进行打印。

How to print the size of a Data in Swift?

如何在 Swift 中打印数据的大小?

回答by Mozahler

Use yourData.count and divide by 1024 * 1024. Using Alexanders excellent suggestion:

使用 yourData.count 并除以 1024 * 1024。使用 Alexanders 的好建议:

    func stackOverflowAnswer() {
      if let data = UIImagePNGRepresentation(#imageLiteral(resourceName: "VanGogh.jpg")) as Data? {
      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)")
      }
    }

With the following results:

结果如下:

There were 28865563 bytes
formatted result: 28.9 MB

回答by Alexander - Reinstate Monica

If your goal is to print the size to the use, use ByteCountFormatter

如果您的目标是打印尺寸以供使用,请使用 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)

回答by abdullahselek

You can use countof Data object and still you can use lengthfor NSData

您可以使用countData 对象,但仍然可以length用于 NSData

回答by AtulParmar

Enter your file URL in the following code to get file size in MB, I hope this helps you.

在以下代码中输入您的文件 URL 以获取以 MB 为单位的文件大小,我希望这对您有所帮助。

let data = NSData(contentsOf: FILE URL)!
let fileSize = Double(data.length / 1048576) //Convert in to MB
print("File size in MB: ", fileSize)

回答by Jovan Stankovic

Following accepted answer I've created simple extension:

按照接受的答案,我创建了简单的扩展:

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))
 }}

回答by Juan Boero

Swift 5.1

斯威夫特 5.1

extension Int {
    var byteSize: String {
        return ByteCountFormatter().string(fromByteCount: Int64(self))
    }
}

Usage:

用法:

let yourData = Data()
print(yourData.count.byteSize)

回答by spnkr

To get the size of a string, adapted from @mozahler's answer

获取字符串的大小,改编自@mozahler 的回答

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)")
}

回答by atreat

countshould suit your needs. You'll need to convert bytes to megabytes (Double(data.count) / pow(1024, 2))

count应该适合您的需求。您需要将字节转换为兆字节 ( Double(data.count) / pow(1024, 2))

回答by Dino Alves

If you want to just see number of bytes, printing the data object directly can give that to you.

如果您只想查看字节数,可以直接打印数据对象。

let dataObject = Data()
print("Size is \(dataObject)")

Should give you:

应该给你:

Size is 0 bytes

In other words, .countwon't be necessary in newer Swift 3.2 or higher.

换句话说,.count在较新的 Swift 3.2 或更高版本中不需要。