Func Filesizeofcache (), Int {
Remove the Cache folder directory cached files are in this directory
Let CachePath =nssearchpathfordirectoriesindomains (FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, True). First
Cache directory path
Remove all file arrays under folder
Let Filearr = FileManager.default.subpaths (atpath:cachepath!)
Quickly enumerate all filenames calculate file size
var size = 0
For file in filearr! {
To stitch the file name into the path
Let Path = (cachepath! as NSString). Appending ("/\ (file)")
Remove file properties
Let Floder = try! FileManager.default.attributesOfItem (Atpath:path)
Using tuples to remove file size properties
For (ABC, BCD) in Floder {
Accumulate file size
If abc = = fileattributekey.size {
Size + = (BCD as Anyobject). IntegerValue
}
}
}
let mm = size/1024/1024
return mm
}
Func ClearCache () {
Remove the Cache folder directory cached files are in this directory
Let CachePath =nssearchpathfordirectoriesindomains (FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, True). First
Remove all file arrays under folder
Let Filearr = FileManager.default.subpaths (atpath:cachepath!)
Traverse Delete
For file in filearr! {
Let Path = (cachepath! as NSString). Appending ("/\ (file)")
If FileManager.default.fileExists (Atpath:path) {
do {
Try FileManager.default.removeItem (Atpath:path)
} catch {
}
}
}
}
Swift Cleanup Cache