1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift:macのディスク使用量を取得する

Last updated at Posted at 2019-06-23
func usage() {
    let url = NSURL(fileURLWithPath: "/")
    let keys: [URLResourceKey] = [.volumeTotalCapacityKey, .volumeAvailableCapacityForImportantUsageKey]
    guard let dict = try? url.resourceValues(forKeys: keys) else {
        return
    }
    let total = (dict[URLResourceKey.volumeTotalCapacityKey] as! NSNumber).int64Value
    let free = (dict[URLResourceKey.volumeAvailableCapacityForImportantUsageKey] as! NSNumber).int64Value

    let totalStr = ByteCountFormatter.string(fromByteCount: total, countStyle: ByteCountFormatter.CountStyle.decimal)
    let freeStr = ByteCountFormatter.string(fromByteCount: free, countStyle: ByteCountFormatter.CountStyle.decimal)
    let usedStr = ByteCountFormatter.string(fromByteCount: total - free, countStyle: ByteCountFormatter.CountStyle.decimal)
    
    Swift.print("Total: \(totalStr), Free: \(freeStr), Used: \(usedStr)")
}

// Total: 1 TB, Free: 637.46 GB, Used: 362.78 GB
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?