// 取出cache文件夾路徑
let cachePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
// 打印路徑,需要測試的可以往這個路徑下放東西
print(cachePath)
// 取出文件夾下所有文件數(shù)組
let files = NSFileManager.defaultManager().subpathsAtPath(cachePath!)
// 用于統(tǒng)計文件夾內(nèi)所有文件大小
var big = Int();
// 快速枚舉取出所有文件名
for p in files!{
// 把文件名拼接到路徑中
let path = cachePath!.stringByAppendingFormat("/\(p)")
// 取出文件屬性
let floder = try! NSFileManager.defaultManager().attributesOfItemAtPath(path)
// 用元組取出文件大小屬性
for (abc,bcd) in floder {
// 只去出文件大小進行拼接
if abc == NSFileSize{
big += bcd.integerValue
}
}
}
// 提示框
let message = "\(big/(1024*1024))M緩存"
let alert = UIAlertController(title: "清除緩存", message: message, preferredStyle: UIAlertControllerStyle.Alert)
let alertConfirm = UIAlertAction(title: "確定", style: UIAlertActionStyle.Default) { (alertConfirm) -> Void in
// 點擊確定時開始刪除
for p in files!{
// 拼接路徑
let path = cachePath!.stringByAppendingFormat("/\(p)")
// 判斷是否可以刪除
if(NSFileManager.defaultManager().fileExistsAtPath(path)){
// 刪除
try! NSFileManager.defaultManager().removeItemAtPath(path)
}
}
}
很簡單記錄mark 一下