// 计算缓存
func setCalculateCacheSize() -> String{
let basePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory,.userDomainMask, true).first
let fileManager = FileManager.default
var isDir:ObjCBool = false
var total:Float = 0
if fileManager.fileExists(atPath: basePath!, isDirectory: &isDir) {
let childrenPath = fileManager.subpaths(atPath: basePath!)
if childrenPath != nil {
for path in childrenPath! {
if !path.contains("Snapshots") {
let childPath = basePath!.appending("/").appending(path)
do {
let attr:NSDictionary = try fileManager.attributesOfItem(atPath: childPath) as NSDictionary
let fileSize = attr["NSFileSize"] as! Float
total += fileSize
}catch {
}
}
}
}
}
let cacheSize = NSString(format: "%0.1f MB缓存", total/1024/1024) as String
return cacheSize
}
//清除缓存
func cleanCache() ->Bool {
let result = true
let basePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
let fileManage = FileManager.default
if fileManage.fileExists(atPath: basePath.first!) {
let childrenPath = fileManage.subpaths(atPath: basePath.first!)
for childPath in childrenPath! {
let cachePath = basePath.first?.appending("/").appending(childPath)
do {
try fileManage.removeItem(atPath: cachePath!)
}catch {
}
}
}
return result
}