zoukankan      html  css  js  c++  java
  • Swift 3.0统计缓存并清理-----(斯威夫特3.0 坑爹之获取文件大小)

    class func clearCache(finshed : (() -> Void)){

            let cachePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)

            let files = NSFileManager.defaultManager().subpathsAtPath(cachePath[0])

            

            let fileManager = NSFileManager.defaultManager()

            

            for tempStr in files!{

                

                let path = (cachePath.first! as NSString).stringByAppendingPathComponent(tempStr)

                // 注释掉的下面的方法无效

                

                echo("testExist:",NSFileManager.defaultManager().fileExistsAtPath(path))

                do {

                    try fileManager.removeItemAtPath(path)

                    if tempStr == files?.last{

                        SVProgressHUD.showImage(nil, status: "清理完成")

                        finshed()

                    }

                } catch let error as NSError{

                    if tempStr == files?.last{

                        SVProgressHUD.showImage(nil, status: "清理完成")

                        finshed()

                    }

                    echo("错误:",error.localizedDescription)

                }

            }

        }

        

        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: "%.2fMB", totalCache / 1024.0 / 1024.0 ) as String

            }

        }

    以上是Swift 2.3版本的方法,坑爹的来了,那就是Swift 3.0不认“NSFileSize”了。。。。

    2.0系列中 fileManager.attributesOfItemAtPath(childPath) 返回的是[String : AnyObject]。

    3.0系列中fileManager.attributesOfItemAtPath(childPath) 返回的是[FileAttributeKey : Any]

  • 相关阅读:
    SignalR的三个Demo
    SignalR的一点点东西
    如何在appconfig中配置服务的ip
    IP分片丢失重传
    以太网之物理层
    以太网数据格式与封装解封
    OSI七层模型与TCP/IP五层模型
    边沿检测方法-FPGA入门教程
    如何用ModelsimSE仿真IP核-以PLL为例
    搭建Modelsim SE仿真环境-使用do文件仿真
  • 原文地址:https://www.cnblogs.com/madarax/p/6203567.html
Copyright © 2011-2022 走看看