硬盘容量获取

swift 3.0

1
2
3
4
5
6
7
8
9
let size = fileManager[FileAttributeKey.systemFreeSize] as! Double //文件系统的大小
let freeSize = fileManager[FileAttributeKey.systemFreeSize] as! Double
freeSize.distance(to: 2)
let sizeGB = size / 1024 / 1024 / 1024
let freeSizeGB = freeSize / 1024 / 1024 / 1024
let sizeGBstr = String(format: "%0.2f", sizeGB)
let freeSizeGBstr = String(format: "%0.2f", freeSizeGB)
diskSpace.text = " 系统总空间 \(sizeGBstr)G. 剩余可用 \(freeSizeGBstr)G"

swift 2.3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func diskmessage(){
let fileManager:NSDictionary = NSFileManager.defaultManager().attributesOfFileSystemForPath(NSHomeDirectory(), error: nil)!
let size = fileManager.objectForKey(NSFileSystemSize) as Double //文件系统的大小
let freeSize = fileManager.objectForKey(NSFileSystemFreeSize) as Double
freeSize.distanceTo(2)
let sizeGB = size / 1024 / 1024 / 1024
let freeSizeGB = freeSize / 1024 / 1024 / 1024
let sizeGBstr = String(format: "%0.2f", sizeGB)//保留两位小数
diskSpace.text = " 系统总空间 \(sizeGBstr)G. 剩余可用 \(freeSizeGB)G"
//println("总大小\(size)空闲剩余\(freeSize)")
}
Author

陈昭

Posted on

2016-08-24

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.