是什么
NSCache 是一個(gè)類似NSDictionary的工具戳杀,當(dāng)內(nèi)存警告的時(shí)候步做,他會(huì)自動(dòng)釋放疚漆。
-源碼
@interface NSCache <KeyType, ObjectType> : NSObject {
@private
id _delegate;
void *_private[5];
void *_reserved;
}
@property (copy) NSString *name;
@property (nullable, assign) id<NSCacheDelegate> delegate;
- (nullable ObjectType)objectForKey:(KeyType)key;
- (void)setObject:(ObjectType)obj forKey:(KeyType)key; // 0 cost
- (void)setObject:(ObjectType)obj forKey:(KeyType)key cost:(NSUInteger)g;
- (void)removeObjectForKey:(KeyType)key;
- (void)removeAllObjects;
@property NSUInteger totalCostLimit; // limits are imprecise/not strict
@property NSUInteger countLimit; // limits are imprecise/not strict
@property BOOL evictsObjectsWithDiscardedContent;
@end
@protocol NSCacheDelegate <NSObject>
@optional
- (void)cache:(NSCache *)cache willEvictObject:(id)obj;
@end
做什么
拿來可以做性能優(yōu)化酣胀,可以做緩存刁赦,搭配本地?cái)?shù)據(jù)庫,在恰當(dāng)?shù)臅r(shí)候?qū)?shù)據(jù)做恰當(dāng)?shù)奶幚怼?/p>
怎么做
1. 初始化 (NSCache)
2. 數(shù)據(jù)操作:- setObject:forKey: (寫)
- objectForKey:(讀)