27 lines
753 B
Swift
27 lines
753 B
Swift
import Foundation
|
|
|
|
extension FileManager {
|
|
|
|
var documentDirectory: URL {
|
|
try! url(
|
|
for: .documentDirectory,
|
|
in: .userDomainMask,
|
|
appropriateFor: nil, create: true)
|
|
}
|
|
|
|
private func fileSizeEnumerator(at directory: URL) -> DirectoryEnumerator? {
|
|
enumerator(at: directory,
|
|
includingPropertiesForKeys: [.totalFileAllocatedSizeKey, .fileAllocatedSizeKey],
|
|
options: []) { (_, error) -> Bool in
|
|
print(error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
func directorySize(_ directory: URL) -> Int {
|
|
return fileSizeEnumerator(at: directory)?
|
|
.compactMap { $0 as? URL }
|
|
.reduce(0) { $0 + $1.fileSize } ?? 0
|
|
}
|
|
}
|