// get your directory url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// check if the url is a directory
if (try? documentsDirectoryURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
print("url is a folder url")
// lets get the folder files
var folderSize = 0
(try? FileManager.default.contentsOfDirectory(at: documentsDirectoryURL, includingPropertiesForKeys: nil))?.lazy.forEach {
folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
}
// format it using NSByteCountFormatter to display it properly
let byteCountFormatter = ByteCountFormatter()
byteCountFormatter.allowedUnits = .useBytes
byteCountFormatter.countStyle = .file
let folderSizeToDisplay = byteCountFormatter.string(for: folderSize) ?? ""
print(folderSizeToDisplay) // "X,XXX,XXX bytes"
}