OC和Swift计算和清除缓存工具类之NSObject,Swift项目

作者:计算机知识
    • 动用NSObject的归类特地总括缓存,封装好的工具类。此分类中使用了八线程和Block作为回调解和管理理。
//
//  KMCacheTool.swift
//  StopSmokingPrograms
//
//  Created by Fran on 15/10/15.
//  Copyright © 2015年 kimree. All rights reserved.
//

import UIKit

class KMCacheTool: NSObject {
    // 计算缓存大小
    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: "%.2f MB", totalCache / 1024.0 / 1024.0 ) as String
        }
    }

    // 清除缓存
    class func clearCache() -> Bool{
        var result = true
        let basePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
        let fileManager = NSFileManager.defaultManager()
        if fileManager.fileExistsAtPath(basePath!){
            let childrenPath = fileManager.subpathsAtPath(basePath!)
            for childPath in childrenPath!{
                let cachePath = basePath?.stringByAppendingString("/").stringByAppendingString(childPath)
                do{
                    try fileManager.removeItemAtPath(cachePath!)
                }catch _{
                    result = false
                }
            }
        }

        return result
    }
}
// NSObject FileManager.h// 04-MGWeChat// Created by ming on 14/12/21.// Copyright © 2014年 ming. All rights reserved.#import <Foundation/Foundation.h>@interface NSObject (FileManager)/** 类方法获取文件的大小 有回调 */  getFileSizeWithFileName:(NSString *)path completion:(NSInteger totalSize))completionBlock;/** 对象方法获取文件的大小 有回调 */- getFileSizeWithFileName:(NSString *)path completion:(NSInteger totalSize))completionBlock;/** 类方法获取caches路径 */  (NSString *)cachesPath;/** 对象方法获取caches路径 */- (NSString *)cachesPath;/** 类方法移除caches 有回调 */  removeCachesWithCompletion:)completionBlock;/** 对象方法移除caches 有回调 */- removeCachesWithCompletion:)completionBlock;@end```- #.m

// NSObject FileManager.m// 04-MGWeChat// Created by ming on 14/13/21.// Copyright © 2014年 ming. All rights reserved.

@implementation NSObject (FileManager)

// 异步方法,无需重回值// 异步方法应用回调,block// 获取文件尺寸

  • getFileSizeWithFileName:(NSString *卡塔尔(قطر‎path completion:(NSInteger totalSize卡塔尔国卡塔尔(قطر‎completionBlock{// 在子线程中总括文件大小dispatch_async(dispatch_get_亚洲必赢56.net,OC和Swift计算和清除缓存工具类之NSObject,Swift项目开发中缓存计算以及清除。global_queue, ^{// 1.文书总大小NSInteger totalSize = 0;

     // 2.创建文件管理者 NSFileManager *fileManager = [NSFileManager defaultManager]; // 3.判断文件存不存在以及是否是文件夹 BOOL isDirectory = NO; BOOL isFileExist = [fileManager fileExistsAtPath:path isDirectory:&isDirectory]; if (!isFileExist) return; // 文件不存在 if (isDirectory) { // 是文件夹 NSArray *subPaths = [fileManager subpathsAtPath:path]; for (NSString *subPath in subPaths) { NSString *filePath = [path stringByAppendingPathComponent:subPath]; BOOL isDirectory = NO; BOOL isExistFile = [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory]; if (!isDirectory && isExistFile && ![filePath containsString:@"DS"]) { totalSize  = [fileManager attributesOfItemAtPath:filePath error:nil].fileSize; } } }else{ // 不是文件夹 totalSize = [fileManager attributesOfItemAtPath:path error:nil].fileSize ; } // 回到主线程 dispatch_sync(dispatch_get_main_queue(), ^{ if (completionBlock) { completionBlock(totalSize); } });
    

    });}

  • getFileSizeWithFileName:(NSString *)path completion:(NSInteger)) completionBlock{[NSObject getFileSizeWithFileName:path completion:completionBlock];}

/** 获取路线 */

  • (NSString *)cachesPath{return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];}
  • (NSString *)cachesPath{return [NSObject cachesPath];}

/** 删除caches */

本文由bwin必赢发布,转载请注明来源

关键词: 随笔 工具 亚洲必赢网址 缓存 OC