swift 3.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

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 清除缓存在每一个应用是一个很常见的功能,今天这里小结一下。 将数据永久性的存储我们称为数据持久化,其本质是将数据存...
    追逐_chase阅读 582评论 0 1
  • //Clojure入门教程: Clojure – Functional Programming for the J...
    葡萄喃喃呓语阅读 3,820评论 0 7
  • 我是一匹马, 生活在青草郁郁的山峡, 那儿的河水漂着野花, 听得到青蛙叫的咕呱, 当夕阳又见红霞, 我倚在拴马桩下...
    观夜阅读 218评论 3 2
  • 春天 燕子的尾巴。 夏天 池里的荷花。 秋天 路边的枫叶。 冬天 窗外的雪花。 我们在雪里相见,你微微一笑,融化了...
    天涯往事阅读 187评论 0 0
  • 昨晚老陈同志(我爹)打电话给我,让我帮忙遥控指挥一下他手机往电脑上传文件,这算是我俩比较和平的一次交流,打完一...
    云梦泽曼曼阅读 500评论 1 1