iOS 清除缓存

/** 获得缓存大小 */
- (NSString *)getCachSize
{
    //总大小
    unsigned long long size = 0;
    
    //得到缓存文件夹路径
    NSString *dirpath = [self dirpath];
    
    //文件管理者
    self.fileManager = [NSFileManager defaultManager];
    
    //得到文件夹的大小
    //得到该路径下的全部文件名字
    NSArray *subpaths = [self.fileManager subpathsAtPath:dirpath];
    
    //累计全部文件大小
    for (NSString *subpath in subpaths) {
        //全路径
        NSString *fulSubpath = [dirpath stringByAppendingPathComponent:subpath];
        //累加文件大小 fileSize字典属性,表示字典大小
        size += [self.fileManager attributesOfItemAtPath:fulSubpath error:nil].fileSize;
    }
    
    
    NSString *sizeStr;
    if (size > 1024 * 1024) {
        size /= 1024 * 1024;
        sizeStr = [NSString stringWithFormat:@"%lluMB",size];
    }else if (size > 1024){
        size /= 1024;
        sizeStr = [NSString stringWithFormat:@"%lluKB",size];
    }else{
        sizeStr = [NSString stringWithFormat:@"%lluB",size];
    }
    return sizeStr;
}

/** 清除缓存 */
- (void)deleteCach
{
    UIAlertView *alert = [[UIAlertView alloc] init];
    alert.title = @"提示";
    [alert addButtonWithTitle:@"肯定"];
    NSString *msg;
    NSString *dirpath = [self dirpath];
    if ([self.fileManager fileExistsAtPath:dirpath]) {
        if ([self.fileManager removeItemAtPath:dirpath error:nil]) {
            msg = @"清楚缓存成功";
        }else
            msg = @"清楚缓存失败";
    }else
            msg = @"缓存不存在";
    alert.message = msg;
    [alert show];
    
    [self.tableView reloadData];
}