宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
//  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
                    }
                }
            }
        }
    }
}