Detailed explanation of the example of judging the size of the calculated cache file in IOS

Time:2021-10-27

Detailed explanation of the example of judging the size of the calculated cache file in IOS

Judging the size of cache files in IOSHere I share my experience and hope to share the technology with you. If there are any deficiencies, please correct them. The purpose of writing this article is to hope that everyone can grow together. I also believe that there is no high or low technology. Only complementarity and sharing can make each other grow more.  

Example code:

//Get cache file path
-(NSString *)getCachesPath{
  //Get cache directory path
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
  NSString *cachesDir = [paths objectAtIndex:0];
  
  NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];
  
  return filePath;
}
///M to calculate the size of the cache file
- (long long) fileSizeAtPath:(NSString*) filePath{
  NSFileManager* manager = [NSFileManager defaultManager];
  if ([manager fileExistsAtPath:filePath]){
    
//// get all file names in a directory
//    NSArray *files = [manager subpathsAtPath:filePath];
//    NSLog(@"files1111111%@ == %ld",files,files.count);
//    
//// obtain the complete file name (with suffix) from the path
//    NSString *exe = [filePath lastPathComponent];
//    NSLog(@"exeexe ====%@",exe);
//    
//// get the file name (without suffix)
//    exe = [exe stringByDeletingPathExtension];
//    
//// get the file name (without suffix)
//    NSString *exestr = [[files objectAtIndex:1] stringByDeletingPathExtension];
//    NSLog(@"files2222222%@ ==== %@",[files objectAtIndex:1],exestr);
    
    
    return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
  }
  
  return 0;
}
- (float ) folderSizeAtPath:(NSString*) folderPath{
  NSFileManager* manager = [NSFileManager defaultManager];
  if (![manager fileExistsAtPath:folderPath]) return 0;
  NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];// Front back enumerator ///
  NSString* fileName;
  long long folderSize = 0;
  while ((fileName = [childFilesEnumerator nextObject]) != nil){
    NSLog(@"fileName ==== %@",fileName);
    NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
    NSLog(@"fileAbsolutePath ==== %@",fileAbsolutePath);
    folderSize += [self fileSizeAtPath:fileAbsolutePath];
  }
  NSLog(@"folderSize ==== %lld",folderSize);
  return folderSize/(1024.0*1024.0);
}
////////////
-(void)ss{
  //Get cache directory path
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
  NSString *cachesDir = [paths objectAtIndex:0];
  
  NSLog(@"cachesDircachesDir == %@",cachesDir);
  //Read the specific single file and / or all files in the cache//
  NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];
  NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
  NSLog(@"filePathfilePath%@ ==array==== %@",filePath, array);
  
  
  NSFileManager* fm=[NSFileManager defaultManager];
  if([fm fileExistsAtPath:filePath]){
    //Get all file names in a directory
    NSArray *files = [fm subpathsAtPath:filePath];
    NSLog(@"files1111111%@ == %ld",files,files.count);
    
    //Get file name (without suffix)
    NSString * exestr = [[files objectAtIndex:1] stringByDeletingPathExtension];
    NSLog(@"files2222222%@ ==== %@",[files objectAtIndex:1],exestr);
  }

}

If you have any questions, please leave a message or go to the community of this site for communication and discussion. Thank you for reading. I hope this article can help you. Thank you for your support to this site!