背景:有一天需要读取“文件”中的文件,我们需要计算文件大小,看了一眼文档。
答案:是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"
}
网友评论