//
|
// CacheFileManager.swift
|
// THProject
|
//
|
// Created by alvin_y on 2018/4/17.
|
// Copyright © 2018年 yang-wang. All rights reserved.
|
//
|
|
import UIKit
|
|
class CacheFileManager: NSObject {
|
|
/// 获取缓存大小
|
class func getCache() -> String {
|
var folderSize:CGFloat = 0
|
let fileManager = FileManager.default
|
let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
|
guard let cahe = cachePath else {return "0"}
|
let files = fileManager.subpaths(atPath: cahe)
|
guard let file = files else {return "0"}
|
for path in file {
|
if path != "Snapshots"{
|
let filesPath = cachePath! + "/\(path)"
|
do {
|
let attr = try fileManager.attributesOfItem(atPath: filesPath)
|
let size = attr[FileAttributeKey.size] as! CGFloat
|
folderSize = folderSize + size
|
|
} catch {
|
print("error :\(error)")
|
}
|
}
|
}
|
return String.init(format: "%.2f", folderSize / 1024.0 / 1024.0)
|
}
|
|
/// 清除缓存
|
class func removeCache() {
|
let fileManager = FileManager.default
|
let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
|
guard let cahe = cachePath else {return }
|
let files = fileManager.subpaths(atPath: cahe)
|
guard let file = files else {return }
|
for path in file {
|
if path != "Snapshots"{
|
let filesPath = cachePath! + "/\(path)"
|
if fileManager.fileExists(atPath: filesPath){
|
do{
|
try fileManager.removeItem(atPath: filesPath)
|
} catch{
|
return
|
}
|
}
|
}
|
}
|
}
|
}
|