NSCache

来源:互联网 发布:正规贵金属行情软件 编辑:程序博客网 时间:2024/05/18 00:05

NSCache

示例代码如下

#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong)NSCache* cache;@end@implementation ViewController-(NSCache *)cache{    if (_cache==nil) {        _cache=[[NSCache alloc]init];        _cache.totalCostLimit=5;    }    return  _cache;}- (IBAction)addBtn:(id)sender {    for (NSInteger i=0; i<10; i++) {        NSData *data=[NSData dataWithContentsOfFile:@"/Users/spencer/Desktop/QQ20170218-175829.png"];        [self.cache setObject:data forKey:@(i) cost:1];        NSLog(@"存入了图片%zd",i);            }            }- (IBAction)checkBtn:(id)sender {    NSLog(@"+++++++++++++++++++++++=");    for (NSInteger i=0; i<10; i++) {                       NSData *data=[self.cache objectForKey:@(i)];        if (data) {            NSLog(@"取出了数据%zd",i);        }            }}- (IBAction)removeAll:(id)sender {    [self.cache removeAllObjects];    }@end


0 0
原创粉丝点击