美文网首页
swift 3.0 缓存计算 ,缓存清除

swift 3.0 缓存计算 ,缓存清除

作者: 周城滨 | 来源:发表于2017-04-26 17:22 被阅读0次

// 计算缓存

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

}

相关文章

网友评论

      本文标题:swift 3.0 缓存计算 ,缓存清除

      本文链接:https://www.haomeiwen.com/subject/kfxwzttx.html