IOS学习 NSCache 缓存类

来源:互联网 发布:网络暴力事件案例分析 编辑:程序博客网 时间:2024/05/18 09:11

#import "ViewController.h"


@interface ViewController ()


@property (nonatomic,strong)NSCache *cache;


@end


@implementation ViewController


-(NSCache *)cache{

    if (_cache ==nil) {

        _cache = [[NSCachealloc]init];

        

        //设置数量限制,删除之前缓存的内容

        _cache.countLimit =15;

        

        //设置代理

        _cache.delegate =self;

    }

    return_cache;

}


//开发测试时使用,一般不用:缓存中的对象将要被删除,调用此方法

-(void)cache:(NSCache *)cache willEvictObject:(id)obj{

    NSLog(@"要删除的对象obj----%@",obj);

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    for (int i =0; i<20; i++) {

        NSString *str = [NSStringstringWithFormat:@"hello - NO.%d",i];

        [self.cachesetObject:str forKey:@(i)];

        /*@(i):[NSNumber numberWithInt:i]

         *@[]:快速加数组

         *@{}:快速加字典    */

    }

    

    for (int i =0; i<20; i++) {

        NSString *str = [self.cacheobjectForKey:@(i)];

        NSLog(@"%@",str);

    }

}


0 0
原创粉丝点击