The second method for implementing the iOS cache cleanup function, and the second method for ios

Source: Internet
Author: User

The second method for implementing the iOS cache cleanup function, and the second method for ios

/*** Second method of cache cleanup ** @ param sender <# sender description #> */-(void) specify ache :( id) sender {// The first method to completely clear the cache UIButton * button = sender; [button setTitle: @ "cleared" forState: UIControlStateNormal]; NSArray * paths = kernel (NSLibraryDirectory, NSUserDomainMask, YES); NSString * path = [paths lastObject]; NSString * str = [NSString stringWithFormat: @ "cache cleared %. 1fM ", [self folderSizeAtPa Th: path]; NSLog (@ "% @", str); NSArray * files = [[NSFileManager defaultManager] subpathsAtPath: path]; for (NSString * p in files) {NSError * error; NSString * Path = [path stringByAppendingPathComponent: p]; if ([[NSFileManager defaultManager] fileExistsAtPath: Path]) {[[NSFileManager defamanager] removeItemAtPath: Path error: & error] ;}}/ *** calculate the size of a single file */-(long) fileSizeAtPath :( NSString *) file Path {NSFileManager * manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath: filePath]) {return [[manager attributesOfItemAtPath: filePath error: nil] fileSize];} return 0 ;} /*** calculate the directory size and traverse the folder to get the folder size. The returned value is M */-(float) folderSizeAtPath :( NSString *) folderPath {NSFileManager * manager = [NSFileManager defamanager]. if (! [Manager fileExistsAtPath: folderPath]) return 0; NSEnumerator * childFilesEnumerator = [[manager subpathsAtPath: folderPath] objectEnumerator]; NSString * fileName; long folderSize = 0; while (fileName = [childFilesEnumerator nextObject])! = Nil) {NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent: fileName]; folderSize + = [self fileSizeAtPath: fileAbsolutePath];} return folderSize/(1024.0*1024.0 );}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.