Class methods for encapsulating caches

Source: Internet
Author: User

Get disk size

//Get Hard disk size+(cgfloat) getdisksize{nsstring*libpath = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) ObjectAtIndex:0]; NSLog (@"%@", LibPath); //Initialize File ManagerNsfilemanager *manager =[Nsfilemanager Defaultmanager]; //get all paths under LibNsarray *liballpath =[manager Subpathsatpath:libpath]; Nsinteger size=0;  for(NSString *pathinchLiballpath) {        if(! [Path containsstring:@"Preferences"]) {            //stitch The path together in fullNSString *patha =[LibPath Stringbyappendingpathcomponent:path]; //get all information about a fileNsdictionary *fileattri =[manager Attributesofitematpath:patha Error:nil]; //Get File SizeSize + = [fileattri[@"nsfilesize"] IntegerValue]; }    }    returnsize/1024.0/1024.0;}

Ways to clear the cache

//Clear Cache+ (void) cleardisk{NSString*libpath = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES) [0]; //Initialize File ManagerNsfilemanager *mgr =[Nsfilemanager Defaultmanager]; //get all paths under LibNsarray *liballpath =[Mgr Subpathsatpath:libpath];  for(NSString *pathinchLiballpath) {        if(! [Path containsstring:@"Preferences"]) {            //stitch The path together in fullNSString *patha =[LibPath Stringbyappendingpathcomponent:path]; //Remove files from[Mgr Removeitematpath:patha Error:nil]; }    }}

The garbage size is displayed in the cell that clears the cache when called

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellidentifier =@"Cell"; UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier]; }    Switch(indexpath.row) { Case 0: Cell.textLabel.text=@"Event Collection";  Break;  Case 1: Cell.textLabel.text=@"Movie Collection";  Break;  Case 2: {cell.textLabel.text=@"Clear Cache"; NSString*size = [NSString stringWithFormat:@"%.2fm", [Cleardiskmanager Getdisksize]]; Cell.detailTextLabel.text=size;  Break; }        default:             Break; } Cell.backgroundcolor=[Uicolor Cyancolor]; returncell;}

Clean up cache when clicking cell

- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{Switch(indexpath.row) { Case 1: {Listtableviewcontroller*LISTVC =[[Listtableviewcontroller alloc] init];        [Self.navigationcontroller PUSHVIEWCONTROLLER:LISTVC Animated:yes];  Break; }         Case 0: {Activitytableviewcontroller*ACTIVITYVC =[[Activitytableviewcontroller alloc] init];            [Self.navigationcontroller PUSHVIEWCONTROLLER:ACTIVITYVC Animated:yes];  Break; }         Case 2: {Uialertcontroller*alercontroller = [Uialertcontroller alertcontrollerwithtitle:@"Tips"Message@"It may take a bit of your traffic to clear the cache"Preferredstyle:uialertcontrollerstylealert]; Uialertaction*action = [Uialertaction actionwithtitle:@"Cancel"Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action)            {[self dismissviewcontrolleranimated:yes completion:nil];            }]; Uialertaction*action1 = [Uialertaction actionwithtitle:@"Determine"Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action)                {[Cleardiskmanager cleardisk];            [Self.tableview Reloaddata];            }];            [Alercontroller addaction:action];            [Alercontroller Addaction:action1];        [Self Presentviewcontroller:alercontroller animated:yes completion:nil]; }        default:             Break; }}

Class methods for encapsulating caches

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.