iOS分享世界复制粘贴Tools

MG--OC和Swift计算和清除缓存工具类之NSObject+

2016-11-20  本文已影响347人  Mg明明就是你



//  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)
/** 类方法获取文件的大小 有回调 */
+ (void)getFileSizeWithFileName:(NSString *)path completion:(void(^)(NSInteger totalSize))completionBlock;
/** 对象方法获取文件的大小 有回调 */
- (void)getFileSizeWithFileName:(NSString *)path completion:(void(^)(NSInteger totalSize))completionBlock;

/** 类方法获取caches路径 */
+ (NSString *)cachesPath;
/** 对象方法获取caches路径 */
- (NSString *)cachesPath;

/** 类方法移除caches 有回调 */
+ (void)removeCachesWithCompletion:(void(^)())completionBlock;
/** 对象方法移除caches 有回调 */
- (void)removeCachesWithCompletion:(void(^)())completionBlock;

@end```

- #.m

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

import "NSObject+FileManager.h"

@implementation NSObject (FileManager)

// 异步方法,不需要返回值
// 异步方法使用回调,block
// 获取文件尺寸

/** 获取路径 */

/** 删除caches */

@end

***
***
***

- 二、#Swift2.x计算和清除缓存的方法
  - 使用NSObject的分类专门计算缓存,封装好的工具类。此分类中使用了多线程和闭包作为回调处理。

// NSObject+FileManager.swift
// Created by ming on 16/10/30.
// Copyright © 2016年 ming. All rights reserved.

import UIKit
/* 计算缓存 /
extension NSObject {
/
类方法:
异步方法
使用回调,block
获取文件尺寸
*/
class func getFileSizeWithFileName(path: String,completionBlock:(totalSize: UInt64) -> () ) {
// 在子线程中计算文件大小
dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
// 1.文件总大小
var totalSize: UInt64 = 0;
// 2.创建文件管理者
let fileManager = NSFileManager.defaultManager()
// 3.判断文件存不存在以及是否是文件夹
var isDirectory: ObjCBool = ObjCBool(false)
let isFileExist = fileManager.fileExistsAtPath(path , isDirectory: &isDirectory)

            if (!isFileExist) {return} // 文件不存在
                if (isDirectory) { // 是文件夹
                    guard let subPaths = fileManager.subpathsAtPath(path) else { return }
                   for subPath in subPaths {
                        let filePath = path.stringByAppendingFormat("/%@", subPath)
                        var isDirectory: ObjCBool = ObjCBool(false)
                        let isExistFile = fileManager.fileExistsAtPath(filePath, isDirectory: &isDirectory)
                        if (!isDirectory && isExistFile && !filePath.containsString("DS")) {
                           if let attr: NSDictionary = try? fileManager.attributesOfItemAtPath(path) {
                                totalSize += attr.fileSize()
                            }
                        }
                    }
           }else{ // 不是文件夹
               if let attr: NSDictionary = try? fileManager.attributesOfItemAtPath(path) {
                   totalSize += attr.fileSize()
               }
           }

           // 回到主线程,把计算的大小通过壁报传递出去
           dispatch_async(dispatch_get_main_queue()) { () -> Void in
                completionBlock(totalSize: totalSize)
           }
     }
}

 // 对象方法:根据缓存路径去计算缓存文件的大小,通过闭包返回缓存大小
func getFileSizeWithFileName(path: String, completionBlock:(totalSize: UInt64) -> ()) {
      NSObject.getFileSizeWithFileName(path, completionBlock:completionBlock)
}

}

/** 获取缓存路径 */
extension NSObject {
class func cachesPath() -> String {
return NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).last!
}

func cachesPath() -> String {
    return NSObject.cachesPath()
}

}

/** 清除缓存 */
extension NSObject {
class func removeCachesWithCompletion(completionBlock: ()->()) {
NSOperationQueue().addOperationWithBlock { () -> Void in
// 创建文件管理者
let fileManager = NSFileManager.defaultManager()

          // 删除文件
          let path = self.cachesPath() as String
          var isDirectory: ObjCBool = ObjCBool(false)
          let isFileExist = fileManager.fileExistsAtPath(path , isDirectory: &isDirectory)

          if (!isFileExist)  { return } // 文件不存在
          if (isDirectory) {
              guard let enumerator = fileManager.enumeratorAtPath(path) else { return }
              for subPath in enumerator {
                  let subPath = subPath as? String
                  let filePath = path.stringByAppendingFormat("/%@", subPath!)
                  // 移除文件Or文件夹
                  try! fileManager.removeItemAtPath(filePath)
              }
          }

          // 回到主线程
          NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
              completionBlock()
        })
     }
}

func removeCachesWithCompletion(completionBlock: ()->()) {
    NSObject.removeCachesWithCompletion(completionBlock)
}

}

***
***
***

- #github

|  项目  |  简介    |  
    | : | : |
    |  [MGDS_Swif](https://github.com/LYM-mg/MGDS_Swift)  |  逗视视频直播 |
    |  [MGMiaoBo](https://github.com/LYM-mg/MGMiaoBo)  |  喵播视频直播 |  
    |  [MGDYZB](https://github.com/LYM-mg/MGDYZB)  |  斗鱼视频直播 |
    |  [MGDemo](https://github.com/LYM-mg/MGDemo)  |  n多小功能合集 |  
    |   [MGBaisi](https://github.com/LYM-mg/MGBaisi)   |  高度仿写百思   | 
    |   [MGSinaWeibo](https://github.com/LYM-mg/MGSinaWeibo)   | 高度仿写Sina   | 
    |   [MGLoveFreshBeen](https://github.com/LYM-mg/MGLoveFreshBeen)   |  一款电商App   | 
    |   [MGWeChat](https://github.com/LYM-mg/MGWeChat)   |  小部分实现微信功能   | 
    |  [MGTrasitionPractice](https://github.com/LYM-mg/MGTrasitionPractice)   |  自定义转场练习   | 
    |  [DBFMDemo](https://github.com/LYM-mg/DBFMDemo)  |  豆瓣电台   | 
    | [MGPlayer](https://github.com/LYM-mg/MGPlayer)  |  一个播放视频的Demo   | 
    |  [MGCollectionView](https://github.com/LYM-mg/MGCollectionView)  |  环形图片排布以及花瓣形排布   | 
    |  [MGPuBuLiuDemo](https://github.com/LYM-mg/MGPuBuLiuDemo)  |  瀑布流--商品展   | 
    |  [MGSlideViewDemo](https://github.com/LYM-mg/MGSlideViewDemo)  |  一个简单点的侧滑效果,仿QQ侧滑   | 
    | [MyResume](https://github.com/LYM-mg/MyResume)  |  一个展示自己个人简历的Demo   | 
    |  [GoodBookDemo](https://github.com/LYM-mg/GoodBookDemo) |  好书   | 

   - #[1、直播喵播MGMiaoBo下载](https://github.com/LYM-mg/MGMiaoBo)
   - #[2、逗视:逗你玩的直播App,可下载试玩](https://github.com/LYM-mg/MGDS_Swift)
  - >#看下效果
![逗视介绍1.gif](http:https://img.haomeiwen.com/i1429890/ecd25e08d367c32e.gif?imageMogr2/auto-orient/strip)
![逗视介绍2.gif](http:https://img.haomeiwen.com/i1429890/91b427263bc09abd.gif?imageMogr2/auto-orient/strip)
***
上一篇下一篇

猜你喜欢

热点阅读