• 封装缓存的类方法


    // 获取磁盘大小

    //获取硬盘大小
    + (CGFloat)getDiskSize
    {
        NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSLog(@"%@", libPath);
        // 初始化文件管理者
        NSFileManager *manager = [NSFileManager defaultManager];
        // 获取lib下所有路径
        NSArray *libAllPath = [manager subpathsAtPath:libPath];
        NSInteger size = 0;
        for (NSString *path in libAllPath) {
            if (![path containsString:@"Preferences"]) {
                //把路径拼接全
                NSString *pathA = [libPath stringByAppendingPathComponent:path];
                //获取文件的所有信息
                NSDictionary *fileAttri = [manager attributesOfItemAtPath:pathA error:nil];
                //获取文件大小
                size += [fileAttri[@"NSFileSize"] integerValue];
            }
        }
        return size/1024.0/1024.0;
    }

    清除缓存的方法

    //清除缓存
    + (void)clearDisk
    {
        NSString *libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
        //初始化文件管理者
        NSFileManager *mgr = [NSFileManager defaultManager];
        //获取lib下所有路径
        NSArray *libAllPath = [mgr subpathsAtPath:libPath];
        
        for (NSString *path in libAllPath) {
            if (![path containsString:@"Preferences"]) {
                //把路径拼接全
                NSString *pathA = [libPath stringByAppendingPathComponent:path];
                //移除文件
                [mgr removeItemAtPath:pathA error:nil];
            }
        }
    }

    调用时  在清除缓存的cell中显示垃圾大小

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *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 = @"活动收藏";
                break;
            case 1:
                cell.textLabel.text = @"电影收藏";
                break;
            case 2:
            {
                cell.textLabel.text = @"清除缓存";
                NSString *size = [NSString stringWithFormat:@"%.2fM",[ClearDiskManager getDiskSize]];
                cell.detailTextLabel.text = size;
                break;
            }
            default:
                break;
        }
        
        cell.backgroundColor = [UIColor cyanColor];
        
        return cell;
    }

    //  点击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:@"提示" message:@"清除缓存之后可能耗费一点你的流量" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" 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;
        }
    }
  • 相关阅读:
    6)图[5]最短路径
    6)图[4]关键路径
    6)图[3]拓扑排序算法
    6)图[2]Prim算法[最小生成树]
    Aprori算法[关联规则算法]
    K-Modes算法[聚类算法]
    linux Centos6.7 python交互模式下回退异常问题解决
    Python-面向对象(二)-Day7
    Python-面向对象(一)-Day7
    (error) MISCONF Redis is configured to save RDB snapshots
  • 原文地址:https://www.cnblogs.com/crazygeek/p/5635951.html
Copyright © 2020-2023  润新知