//
// KMCacheTool.swift
// StopSmokingPrograms
//
// Created by Fran on 15/10/15.
// Copyright © 2015年 kimree. All rights reserved.
//
import UIKit
class KMCacheTool: NSObject {
// 计算缓存大小
static var cacheSize: String{
get{
let basePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
let fileManager = NSFileManager.defaultManager()
func caculateCache() -> Float{
var total: Float = 0
if fileManager.fileExistsAtPath(basePath!){
let childrenPath = fileManager.subpathsAtPath(basePath!)
if childrenPath != nil{
for path in childrenPath!{
let childPath = basePath!.stringByAppendingString("/").stringByAppendingString(path)
do{
let attr = try fileManager.attributesOfItemAtPath(childPath)
let fileSize = attr["NSFileSize"] as! Float
total += fileSize
}catch _{
}
}
}
}
return total
}
let totalCache = caculateCache()
return NSString(format: "%.2f MB", totalCache / 1024.0 / 1024.0 ) as String
}
}
// 清除缓存
class func clearCache() -> Bool{
var result = true
let basePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(basePath!){
let childrenPath = fileManager.subpathsAtPath(basePath!)
for childPath in childrenPath!{
let cachePath = basePath?.stringByAppendingString("/").stringByAppendingString(childPath)
do{
try fileManager.removeItemAtPath(cachePath!)
}catch _{
result = false
}
}
}
return result
}
}
Swift项目开发中缓存计算以及清除
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 接着上篇下载word/pdf/txt等文档的文章,此篇涉及到清除这些文档。本文实现文件/文件夹大小的计算以及清除文...