美文网首页
iOS 中Data的count属性单位

iOS 中Data的count属性单位

作者: 東玖零 | 来源:发表于2022-10-18 13:47 被阅读0次

背景:有一天需要读取“文件”中的文件,我们需要计算文件大小,看了一眼文档。

答案:是byte(字节)。(The number of bytes in the data.)

源码和源码注释如下:

    /// The number of bytes in the data.
    @inlinable public var count: Int

记录个方法来计算大小:

    static func paresSize(count:Int) -> String {
        let gb = 1024 * 1024 * 1024 //定义GB的计算常量
        let mb = 1024 * 1024 //定义MB的计算常量
        let kb = 1024 //定义KB的计算常量
        if count/gb >= 1 {
            return String(roundf(Float(count)/Float(gb))) + "GB"
        }
        if count/mb >= 1 {
            return String(roundf(Float(count)/Float(mb))) + "MB"
        }
        if count/kb >= 1 {
            return String(roundf(Float(count)/Float(kb))) + "KB"
        }
        return String(count) + "Byte"
    }

相关文章

网友评论

      本文标题:iOS 中Data的count属性单位

      本文链接:https://www.haomeiwen.com/subject/yszmirtx.html