NSCache: 專門做緩存的類
官方描述
A mutable collection you use to temporarily store transient key-value pairs that are subject to eviction when resources are low.
一個可變的集合酸役,用于臨時存儲在資源不足時易被驅(qū)逐的臨時鍵值對住诸。
NSCache屬性和方法介紹
#import <Foundation/NSObject.h>
@class NSString;
@protocol NSCacheDelegate;
NS_HEADER_AUDIT_BEGIN(nullability, sendability)
API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0))
@interface NSCache <KeyType, ObjectType> : NSObject
@property (copy) NSString *name;// 名稱
@property (nullable, assign) id<NSCacheDelegate> delegate;// 設(shè)置代理
- (nullable ObjectType)objectForKey:(KeyType)key;
//在緩存中設(shè)置指定鍵名對應(yīng)的值,0成本
- (void)setObject:(ObjectType)obj forKey:(KeyType)key; // 0 cost
//在緩存中設(shè)置指定鍵名對應(yīng)的值涣澡,并且指定該鍵值對的成本贱呐,用于計算記錄在緩存中的所有對象的總成本
- (void)setObject:(ObjectType)obj forKey:(KeyType)key cost:(NSUInteger)g;
//刪除緩存中指定鍵名的對象
- (void)removeObjectForKey:(KeyType)key;
//刪除緩存中所有的對象
- (void)removeAllObjects;
@property NSUInteger totalCostLimit; // 緩存空間的最大總成本,超出上限會自動回收對象入桂。默認值為0奄薇,表示沒有限制
@property NSUInteger countLimit; // 能夠緩存的對象的最大數(shù)量。默認值為0抗愁,表示沒有限制
@property BOOL evictsObjectsWithDiscardedContent;
@end
//該代理只有一個回調(diào)方法馁蒂,移除存儲內(nèi)容的時候會執(zhí)行代理方法呵晚。
@protocol NSCacheDelegate <NSObject>
@optional
/* Called when an object is about to be evicted or removed from the cache.
當要從緩存中清除或刪除對象時調(diào)用。*/
- (void)cache:(NSCache *)cache willEvictObject:(id)obj;
@end
NSCache 特點:1.會自動釋放對象 2.只Strong 不Copy 3.線程安全
1)NSCache是蘋果官方提供的緩存類远搪,具體使用和NSMutableDictionary類似劣纲,在AFN和SDWebImage框架中被使用來管理緩存
2)蘋果官方解釋NSCache在系統(tǒng)內(nèi)存很低時谁鳍,會自動釋放對象.
建議:接收到內(nèi)存警告時主動調(diào)用removeAllObject方法釋放對象
3)NSCache的Key只是對對象進行Strong引用,不是拷貝绷柒,在清理的時候計算的是實際大小而不是引用的大小
4)NSCache是線程安全的废睦,在多線程操作中养泡,不需要對NSCache加鎖.NSDictionary不是。在開發(fā)者自己不編寫加鎖代碼的前提下购披,多個線程可以同時訪問NSCache肩榕。對緩存來說,線程安全通常是很重要的筐乳,因為開發(fā)者可能在某個線程中讀取數(shù)據(jù)乔妈,此時如果發(fā)現(xiàn)緩存里找不著指定的鍵,那么就要下載該鍵對應(yīng)的數(shù)據(jù)了
NSDictionary 字典集合對象
1)NSDictionary 鍵(key)是被拷貝的并且需要是恒定的贮懈,通常我們用字符串類型對象作為鍵比較多优训,其他的對象也可以作為鍵各聘,但是對象要遵守<NSCopying> 協(xié)議實現(xiàn)相應(yīng)的copy方法即可。
2)NSDictionary 對值(value)的是通過強引用來存儲值對象的
3)NSDictionary 不是線程安全的躲因,多線程訪問需要程序員自己編寫保證線程安全的代碼
4)NSDictionary 所占內(nèi)存不會像NSCache那樣被系統(tǒng)自動清除,而是需要程序員自己處理內(nèi)存使用問題
這里順便提一嘴:setObject:ForKey:與setValue:ForKey:的區(qū)別與聯(lián)系
1.setObject:ForKey:是NSMutableDictionary特有的水孩;
setValue:ForKey:是KVC的主要方法琐驴;
2.setObject:ForKey:中object不能為nil,不然會報錯 绝淡;key的參數(shù)只要是對象就可以牢酵,不局限于NSString ;
setValue:ForKey:中Value可以為nil布近,此時會自動調(diào)用removeObject:forKey:方法丝格;key的參數(shù)只能是NSString類型 ;
3.nil與null不同季蚂,[NSNull null]表示是的一個空的對象扭屁,并不是nil涩禀;
4.setValue:ForKey:是在NSObject對象中創(chuàng)建的,即所有的對象都有這個方法葵腹,可以用于任何類屿岂;(方法調(diào)用者是對象的時候)
好到這這里提個問題: 下面打印的值分別是多少爷怀?
NSDictionary *dict =@{@"name":@"liuyunfei",@"@age":@"18",@"age":@"19"};
NSLog(@"age1:%@",[dict valueForKey:@"@age"]);
NSLog(@"age2:%@",[dict valueForKey:@"age"]);
NSLog(@"age3:%@",[dict objectForKey:@"@age"]);
NSLog(@"age4:%@",[dict objectForKey:@"age"]);
NSMapTable:相對NSDictionary來說對key和value具有更廣泛的語義說明,可以指定key或value是拷貝烤惊、強引用或弱引用。
@interface NSMapTable<KeyType, ObjectType> : NSObject <NSCopying, NSSecureCoding, NSFastEnumeration>
// 初始化方法渡贾,可以對 key 和 value 設(shè)置引用策略空骚, initialCapacity:這個是初始化容積數(shù)量
- (instancetype)initWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions capacity:(NSUInteger)initialCapacity NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithKeyPointerFunctions:(NSPointerFunctions *)keyFunctions valuePointerFunctions:(NSPointerFunctions *)valueFunctions capacity:(NSUInteger)initialCapacity NS_DESIGNATED_INITIALIZER;
+ (NSMapTable<KeyType, ObjectType> *)mapTableWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions;
+ (NSMapTable<KeyType, ObjectType> *)strongToStrongObjectsMapTable API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
+ (NSMapTable<KeyType, ObjectType> *)weakToStrongObjectsMapTable API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); // entries are not necessarily purged right away when the weak key is reclaimed
+ (NSMapTable<KeyType, ObjectType> *)strongToWeakObjectsMapTable API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
+ (NSMapTable<KeyType, ObjectType> *)weakToWeakObjectsMapTable API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)); // entries are not necessarily purged right away when the weak key or object is reclaimed
/* return an NSPointerFunctions object reflecting the functions in use. This is a new autoreleased object that can be subsequently modified and/or used directly in the creation of other pointer "collections". */
@property (readonly, copy) NSPointerFunctions *keyPointerFunctions;
@property (readonly, copy) NSPointerFunctions *valuePointerFunctions;
- (nullable ObjectType)objectForKey:(nullable KeyType)aKey;
- (void)removeObjectForKey:(nullable KeyType)aKey;
- (void)setObject:(nullable ObjectType)anObject forKey:(nullable KeyType)aKey; // add/replace value (CFDictionarySetValue, NSMapInsert)
@property (readonly) NSUInteger count;
- (NSEnumerator<KeyType> *)keyEnumerator;
- (nullable NSEnumerator<ObjectType> *)objectEnumerator;
- (void)removeAllObjects;
- (NSDictionary<KeyType, ObjectType> *)dictionaryRepresentation; // create a dictionary of contents
@end