In some software, the user will generally show the memory of the current app, while the user can according to their own needs to clear the cached content. In general, the folder is not the size of this property, so you need to traverse the contents of the folder to calculate the size of the folder, the following with Nsfilemanger to achieve this function.
Learn that the file/folder path is a nsstring string type, so you can add a taxonomy to a string if it is a file/folder implementation that calculates its size. The classification is named FileSize.
1-(Nsinteger) filesize{2 //File Manager3Nsfilemanager *mgr =[Nsfilemanager Defaultmanager];4 //determine if a string is a file/folder5BOOL dir =NO;6BOOL exists = [Mgr Fileexistsatpath:self isdirctory:&dir];7 //File/folder does not exist8 if(exists = = NO)return 0;9 //Self is a folderTen if(dir) { One //traverse all content in a folder ANsarray *subpaths =[Mgr Subpathsatpath:self]; - //Calculate Folder Size -Nsinteger totalbytesize =0; the for(NSString *subpathinchsubpaths) { - //Stitching Full path -NSString *fullsubpath =[self stringbyappendingpathcomponent:subpath]; - //determine if the file is +BOOL dir =NO; -[Mgr Fileexistsatpath:fullsubpath isdirectory:&dir]; + if(dir = = NO) {//is a file ANsdictionary *attr =[Mgr Attributesoffitematpath:fullsubpath error:ni]; atTotalbytesize + =[attr[nsfilesize] integervalue]; - } - } - returntotalbytesize; -}Else{//is a file -Nsdictionary *attr =[Mgr Attributesoffitematpath:self error:ni]; in return[attr[nsfilesize] integervalue]; - } to}
This allows for the calculation of file/folder size
For example, calculate the size of the caches file
1 nsstring *caches =[[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) Lastobject]; 2 Integer cachessize = [caches fileSize];
This will give you the size of the caches folder.
If the input string is not a file/folder, get 0.
Nsfilemanager Calculate File/Folder size