ios之NSCache的使用
来源:互联网 发布:网络医疗 编辑:程序博客网 时间:2024/05/17 09:35
#import "ViewController.h"
@interface ViewController () <NSCacheDelegate>
/// 全局的缓存池
@property (nonatomic,strong)NSCache *cache;
@end
@implementation ViewController
- (NSCache *)cache
{
if (_cache ==nil) {
_cache = [[NSCachealloc] init];
_cache.delegate =self;
// 设置缓存的个数(限额)
_cache.countLimit =5;
}
return_cache;
}
- (void)viewDidLoad {
[superviewDidLoad];
// 循环的向cache中添加对象
for (int i =0; i < 10; i++) {
NSString *str = [NSStringstringWithFormat:@"hello_%d",i+1];
NSLog(@"添加 %@",str);
NSString *key = [NSStringstringWithFormat:@"key_%d",i];
[self.cachesetObject:str forKey:key];
}
// 循环的取值
for (int i =0; i < 10; i++) {
NSString *key = [NSStringstringWithFormat:@"key_%d",i];
NSString *str = [self.cacheobjectForKey:key];
NSLog(@"获取 %@",str);
}
}
/// NSCache的代理方法,在对象将要从cache中移除的时候调用的
- (void)cache:(NSCache *)cache willEvictObject:(id)obj
{
NSLog(@"移除 %@",obj);
}
/// 在 didReceiveMemoryWarning方法中,移除了所有对象之后,再去做添加的操作
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
for (int i =0; i < 10; i++) {
NSString *str = [NSStringstringWithFormat:@"hello_%d",i+1];
NSLog(@"touchesBegan添加 %@",str);
NSString *key = [NSStringstringWithFormat:@"key_%d",i];
[self.cachesetObject:str forKey:key];
}
// 循环的取值
for (int i =0; i < 10; i++) {
NSString *key = [NSStringstringWithFormat:@"key_%d",i];
NSString *str = [self.cacheobjectForKey:key];
NSLog(@"touchesBegan获取 %@",str);
}
}
/// 此时会再重新的添加
- (IBAction)clickBtn:(id)sender
{
[self.cacheremoveAllObjects];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// 注意 :一旦在 didReceiveMemoryWarning 调用移除全部对象的方法之后,那么这个cache永远不会再去添加对象
[self.cacheremoveAllObjects];
}
- ios之NSCache的使用
- iOS---NSCache的简单使用
- IOS之NSCache解析
- iOS之NSCache介绍
- iOS NSCache使用
- NSCache的简单使用
- 关于NSCache的使用
- NSCache的基本使用
- NSCache在IOS中的使用
- NSCache的简单使用介绍
- iOS学习之——NSCache
- iOS学习笔记-130.NSCache使用
- iOS NSCache缓存类的详解
- iOS 如何在程序中使用NSCache实现缓存
- iOS 【Mutithreading-NSCache】
- iOS NSCache 用法
- ios-NSCache简单介绍
- iOS NSCache内存优化
- NOIP 2005 普及组 复赛 apple 淘淘摘苹果
- android 圆形虚线背景
- 建造器模式(builder)
- cpp——复合类型——函数——运算符重载
- TP框架实现发送邮件
- ios之NSCache的使用
- Java集合---ConcurrentHashMap原理分析
- 图像拼接(八):拼接多幅图像+Matlab实现+Stanford Open Course
- bootstrap-进度条
- Java面试整理 关于hibernate
- 为什么要使用工厂模式
- 内连接、左外连接、右外连接、交叉连接区别
- 利用poi从jsp页面导出报表到excel
- Java 参数split分割