博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
封装NSMapTable并简易的使用
阅读量:6212 次
发布时间:2019-06-21

本文共 2638 字,大约阅读时间需要 8 分钟。

封装NSMapTable并简易的使用

NSMapTable是弱引用的字典,可以用来存储对象,该对象消失了也没有关系,对于控制器越级跳转相当有用:)

WeakDictionary.h 与 WeakDictionary.m

////  WeakDictionary.h//  弱引用字典////  http://www.cnblogs.com/YouXianMing///  Copyright (c) 2014年 Y.X. All rights reserved.//#import 
#define GET_WEAK_OBJECT(key) [WeakDictionary objectForKey:(key)]#define REGISTER_WEAK_OBJECT(object, key) [WeakDictionary addObject:(object) forKey:(key)]@interface WeakDictionary : NSObject+ (void)addObject:(id)object forKey:(NSString *)key;+ (id)objectForKey:(NSString *)key;@end
////  WeakDictionary.m//  弱引用字典////  http://www.cnblogs.com/YouXianMing///  Copyright (c) 2014年 Y.X. All rights reserved.//#import "WeakDictionary.h"static NSMapTable  *weakDictionary = nil;@implementation WeakDictionary+ (void)initialize{    if (self == [WeakDictionary class])    {        // 强引用key值弱引用object        weakDictionary = [NSMapTable strongToWeakObjectsMapTable];    }}+ (void)addObject:(id)object forKey:(NSString *)key{    if (object == nil || key == nil)    {        NSLog(@"object or key should not be nil.");        return;    }        if ([weakDictionary objectForKey:key] == nil)    {        [weakDictionary setObject:object forKey:key];    }}+ (id)objectForKey:(NSString *)key{    return [weakDictionary objectForKey:key];}@end

提供的两个category

NSObject+WeakDictionary.h 与 NSObject+WeakDictionary.m

////  NSObject+WeakDictionary.h//  YouXianMing////  Created by YouXianMing on 14-9-20.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
@interface NSObject (WeakDictionary)- (void)storeToWeakDictionaryWithKey:(NSString *)key;@end
////  NSObject+WeakDictionary.m//  YouXianMing////  Created by YouXianMing on 14-9-20.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "NSObject+WeakDictionary.h"#import "WeakDictionary.h"@implementation NSObject (WeakDictionary)- (void)storeToWeakDictionaryWithKey:(NSString *)key{    [WeakDictionary addObject:self forKey:key];}@end

NSString+WeakDictionary.h 与 NSString+WeakDictionary.m

////  NSString+WeakDictionary.h//  YouXianMing////  Created by YouXianMing on 14-9-20.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
@interface NSString (WeakDictionary)- (id)recoverFromWeakDictionary;@end
////  NSString+WeakDictionary.m//  YouXianMing////  Created by YouXianMing on 14-9-20.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "NSString+WeakDictionary.h"#import "WeakDictionary.h"@implementation NSString (WeakDictionary)- (id)recoverFromWeakDictionary{    return [WeakDictionary objectForKey:self];}@end

使用示例的话,以上一篇教程为例子来演示: 

 

你可能感兴趣的文章
[剑指offer] 矩阵中的路径
查看>>
log4j2配置
查看>>
07.Java基础(泛型)
查看>>
自制操作系统Antz day11——实现shell(下)命令响应
查看>>
MyBatis 笔记
查看>>
常见浏览器User-Agent大全
查看>>
第七章:Redis 位图bitmap&基数统计HyperLogLog
查看>>
声优直播互动平台克拉克拉获1.2亿元融资,将布局虚拟偶像互动
查看>>
poj 1154 letters (dfs回溯)
查看>>
php缓冲区与header函数之间的秘密
查看>>
Xcode 10: Build input file double-conversion cannot be found
查看>>
润乾V5与springBoot集成
查看>>
2018-07-26雨
查看>>
新的 TLS 加密破坏攻击也会影响新的 TLS 1.3 协议
查看>>
情人节·送什么给你的程序员男友?
查看>>
玩转算法面试(一)
查看>>
Tomcat工作原理
查看>>
决策树模型算法研究与案例分析
查看>>
Derek解读Bytom源码-孤块管理
查看>>
Kotlin 使用 Spring WebFlux 实现响应式编程
查看>>