• iOS 获取本地缓存文件大小及清除


    由于项目需求中要求计算出应用内的缓存文件的大小及清除工作,做了一个小小的模块提供给大家分享,随便说句买苹果还是选个16g以上的那样妈妈就不会担心你的内存不用够用了哦! 

    // 清除本地缓存文件

    + (void)clearCacheFile

    {

        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];

        for (NSString *fileName in files)

        {

            NSError *error;

            NSString *path = [cachPath stringByAppendingPathComponent:fileName];

            if ([[NSFileManager defaultManager] fileExistsAtPath:path])

            {

                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];

            }

        }

    }

     

    // 计算本地缓存文件大小

    + (double)getCacheFileSize

    {

        NSFileManager *fileManager = [NSFileManager defaultManager];

        

        double fileSize = 0.0;

        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSArray *files = [fileManager subpathsAtPath:cachPath];

        for (NSString *fileName in files)

        {

            NSString *path = [cachPath stringByAppendingPathComponent:fileName];

            if ([fileManager fileExistsAtPath:path])

            {

                NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];

                fileSize += (double)([fileAttributes fileSize]);

            }

        }

        

        return fileSize;

    }

  • 相关阅读:
    Spark-sql windows 下 执行错误.
    notepad ++ 注册表
    log4j 配置文件 示例
    linux 查看 进程 内存占用
    spring boot 常见错误解决
    python 轻量 web 框架 Bottle 使用
    Spring cloud eureka 添加 spring-security
    vue can‘ not resolver sass-loader 的 解决办法。
    外国人眼中的珍珠奶茶是啥?
    75.2亿美元:诺基亚、微软终于在一起
  • 原文地址:https://www.cnblogs.com/zero-zql/p/4806526.html
Copyright © 2020-2023  润新知