Swift4下统计缓存、清除缓存

//WLHUDTools是HUD工具,视情况替换
 func clean(){
        
        WLHUDTools.shareTools.show()
        
        DispatchQueue.global().async {
            // 取出cache文件夹路径
            let cachePath = NSSearchPathForDirectoriesInDomains(
                .cachesDirectory, .userDomainMask, true).first
            
            // 取出文件夹下所有文件数组
            let files = FileManager.default.subpaths(atPath: cachePath!)
            
            // 用于统计文件夹内所有文件大小
            var big = Int();
            
            // 快速枚举取出所有文件名
            for p in files!{
                // 把文件名拼接到路径中
                let path = cachePath!.appendingFormat("/\(p)")
                
                // 取出文件属性
                if let floder = try? FileManager.default.attributesOfItem(atPath: path){
                    
                    // 用元组取出文件大小属性
                    for (abc,bcd) in floder {
                        // 只去出文件大小进行拼接
                        if abc == FileAttributeKey.size{
                            big += (bcd as AnyObject).integerValue
                        }
                    }
                }
            }
            
            // 提示框
            let message = "\(big/(1024*1024))M缓存"
            
            DispatchQueue.main.async {
                
                WLHUDTools.shareTools.hide()
                
                let alert = UIAlertController(title: "清除缓存", message: message, preferredStyle: UIAlertControllerStyle.alert)
                
                let alertConfirm = UIAlertAction(title: "确定", style: UIAlertActionStyle.default) { (alertConfirm) -> Void in
                    
                    if let files = files{
                        
                        if files.isEmpty{
                            WLHUDTools.shareTools.showText(text: "清除成功~")
                        }
                        // 点击确定时开始删除
                        for p in files{
                            // 拼接路径
                            let path = cachePath!.appendingFormat("/\(p)")
                            
                            if(FileManager.default.fileExists(atPath: path) && FileManager.default.isDeletableFile(atPath: path)){
                                do {
                                    try FileManager.default.removeItem(atPath: path as String)
                                    
                                    WLHUDTools.shareTools.showText(text: "清除成功~")
                                } catch {
                                    print("removeItemAtPath err"+path)
                                }
                            }
                        }
                   
                    }else{
                        WLHUDTools.shareTools.showText(text: "清除成功~")
                    }
                }
                
                alert.addAction(alertConfirm)
                
                let cancle = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) { (cancle) -> Void in
                    
                }
                
                alert.addAction(cancle)
                // 提示框弹出
                self.present(alert, animated: true) { () -> Void in
                    
                }
            }
        }
    }

你可能感兴趣的:(Swift4下统计缓存、清除缓存)