I have been calculating directory sizes using subpathsOfDirectoryAtPath::
and have found it very sluggish. After copious amounts of research I came across the stat
structure that can be used to achieve the desired results more quickly and efficiently. How can I use stat
in swift to achieve this goal?
Here's my current file size calculation algorithm using subpathsOfDirectoryAtPath::
:
func calcSize(pathToDir: String) -> (Int) {
let fm = NSFileManager.defaultManager()
var size = 0
let fileArray = (fm.subpathsOfDirectoryAtPath(pathToDir, error: nil)) as! [String]
for file in fileArray {
autoreleasepool({
var fileDict: NSDictionary? = fm.attributesOfItemAtPath(pathToDir.stringByAppendingPathComponent(file), error: nil)
size += Int(fileDict!.fileSize())
})
}
return (size)
}
PS. all the results I found so far have answers in objective-c and C but not in swift.
Here is the sample usage in Swift:
EDIT