Coder Social home page Coder Social logo

clearcache-swift's Introduction

ClearCache-Swift

iOS-Swift-清理缓存 #前言 ####swift现在网上资源比较乱,需要找的要找很久,于是自己开始整理自己遇到的功能开始swift化,如果效果不错的话以后就会一直发表swift的博客,所以请各位读者多给我的GitHub点几个Star吧!

#效果图 这里写图片描述 #上代码 ####在storyBoard创建一个button关联点击事件,然后在ViewController里写上以下代码

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
}

@IBAction func alertAction(sender: UIButton) {
    // 取出cache文件夹路径
    let cachePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
    // 打印路径,需要测试的可以往这个路径下放东西
    print(cachePath)
    // 取出文件夹下所有文件数组
    let files = NSFileManager.defaultManager().subpathsAtPath(cachePath!)
    // 用于统计文件夹内所有文件大小
    var big = Int();
    
    
    // 快速枚举取出所有文件名
    for p in files!{
        // 把文件名拼接到路径中
        let path = cachePath!.stringByAppendingFormat("/\(p)")
        // 取出文件属性
        let floder = try! NSFileManager.defaultManager().attributesOfItemAtPath(path)
        // 用元组取出文件大小属性
        for (abc,bcd) in floder {
            // 只去出文件大小进行拼接
            if abc == NSFileSize{
                big += bcd.integerValue
            }
        }
    }
    
    // 提示框
    let message = "\(big/(1024*1024))M缓存"
    let alert = UIAlertController(title: "清除缓存", message: message, preferredStyle: UIAlertControllerStyle.Alert)
    
    let alertConfirm = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (alertConfirm) -> Void in
        // 点击确定时开始删除
        for p in files!{
            // 拼接路径
            let path = cachePath!.stringByAppendingFormat("/\(p)")
            // 判断是否可以删除
            if(NSFileManager.defaultManager().fileExistsAtPath(path)){
                // 删除
                try! NSFileManager.defaultManager().removeItemAtPath(path)
            }
        }
    }
    alert.addAction(alertConfirm)
    let cancle = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel) { (cancle) -> Void in
        
    }
    alert.addAction(cancle)
    // 提示框弹出
    presentViewController(alert, animated: true) { () -> Void in
        
    }
}

#效果展示 这里写图片描述

本人还是一只小菜鸡,不过是一只热心肠的菜鸡,如果有需要帮助或者代码中有更好的建议的话可以发邮件到[email protected]中,我们一起进步XD

clearcache-swift's People

Contributors

lafree317 avatar

Watchers

James Cloos avatar peterwang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.