NSHashTable和NSMapTable用法
2016-05-09 15:54
162 查看
转载自:简书 http://www.jianshu.com/p/de71385930ba
一个项目中的需求
在iOS项目开发过程中,我们经常会使用到NSSet、
NSArray、
NSDictionary三个类,它们为我们设计较友好的数据结构时提供了很方便的方法先准备本文中将要使用的对象:
#import <Foundation/Foundation.h> @interface HHHuman : NSObject @property (nonatomic ,strong) NSString *name; + (instancetype) humanWithName:(NSString *)n; @end @implementation HHHuman + (instancetype) humanWithName:(NSString *)n { HHHuman *human = [[HHHuman alloc] init]; human.name = n; return [human autorelease]; } - (NSString *)description { return [NSString stringWithFormat:@"%@'s retainCount is %lu",self.name,[self retainCount]]; } - (void)dealloc { self.name = nil; [super dealloc]; } @end在程序开发过程中,经常会用到诸如此类的
Model对象.用法呢也大致会有如下几种方式:1.通过有序的数列进行存储,数组
NSArray;
HHHuman *human_1 = [HHHuman humanWithName:@"lilei"]; HHHuman *human_2 = [HHHuman humanWithName:@"hanmeimei"]; HHHuman *human_3 = [HHHuman humanWithName:@"lewis"]; HHHuman *human_4 = [HHHuman humanWithName:@"xiaohao"]; HHHuman *human_5 = [HHHuman humanWithName:@"beijing"]; id list = @[human_1,human_2,human_3,human_4,human_5]; NSLog(@"%@",list);输出的结果如下:
( "lilei's retainCount is 2", "hanmeimei's retainCount is 2", "lewis's retainCount is 2", "xiaohao's retainCount is 2", "beijing's retainCount is 2" )2.通过统一的关键字进行存储,字典
NSDictionary;
HHHuman *human_1 = [HHHuman humanWithName:@"lilei"]; HHHuman *human_2 = [HHHuman humanWithName:@"hanmeimei"]; HHHuman *human_3 = [HHHuman humanWithName:@"lewis"]; HHHuman *human_4 = [HHHuman humanWithName:@"xiaohao"]; HHHuman *human_5 = [HHHuman humanWithName:@"beijing"]; id dic = @{@"excellent":human_1}; //同样在控制台输出上文字典,用来查看每个对象的保留值 NSLog(@"%@",list);输出的结果如下:
( "lilei's retainCount is 3", "hanmeimei's retainCount is 3", "lewis's retainCount is 2", "xiaohao's retainCount is 2", "beijing's retainCount is 2" )通过上述两个例子我们能够发现一个问题,即
将对象添加到容器时,会对该对象的引用技术+1这样就会有可能发生循环持有的问题,例如如下代码:
@interface HHHuman : NSObject @property (nonatomic ,strong) NSString *name; @property (nonatomic ,strong) NSMutableArray *family; + (instancetype) humanWithName:(NSString *)n; @end @implementation HHHuman + (instancetype) humanWithName:(NSString *)n { HHHuman *human = [[HHHuman alloc] init]; human.name = n; human.family = [[NSMutableArray alloc] init]; [human.family addObject:human]; return [human autorelease]; } - (NSString *)description { return [NSString stringWithFormat:@"%@'s retainCount is %lu",self.name,[self retainCount]]; } - (void)dealloc { self.name = nil; self.family = nil; [super dealloc]; } @end在以上代码中,一个
human的实例对象中包含一个
strong修饰的
family属性,但是在
family属性中,又添加了
human自身对象,这样会造成循环持有的问题,而导致内存泄漏。但是项目需求又要求我们在该
Model对象中完成如此代码,我们不得已会多创建一个类
HHHumanRelationShip,如下所示:
#import <Foundation/Foundation.h> @interface HHHuman : NSObject @property (nonatomic ,strong) NSString *name; + (instancetype) humanWithName:(NSString *)n; @end @implementation HHHuman + (instancetype) humanWithName:(NSString *)n { HHHuman *human = [[HHHuman alloc] init]; human.name = n; return [human autorelease]; } - (NSString *)description { return [NSString stringWithFormat:@"%@'s retainCount is %lu",self.name,[self retainCount]]; } - (void)dealloc { self.name = nil; [super dealloc]; } @end @interface HHHumanRelationShip : NSObject @property (nonatomic ,strong) HHHuman *human; @property (nonatomic ,strong) NSArray *family; + (instancetype)relationShipWithHuman:(HHHuman *)human family:(NSArray *)members; @end @implementation HHHumanRelationShip + (instancetype)relationShipWithHuman:(HHHuman *)human family:(NSArray *)members { HHHumanRelationShip *rs = [[HHHumanRelationShip alloc] init]; rs.human = human; rs.family = members; return [rs autorelease]; } - (NSString *)description { return [NSString stringWithFormat:@"%@'s family's member is %@",self.human,self.family]; } - (void)dealloc { self.human = nil; self.family = nil; [super dealloc]; } @end int main(int argc, const char * argv[]) { HHHuman *human_0 = [HHHuman humanWithName:@"parent"]; HHHuman *human_1 = [HHHuman humanWithName:@"lilei"]; HHHuman *human_2 = [HHHuman humanWithName:@"hanmeimei"]; HHHuman *human_3 = [HHHuman humanWithName:@"lewis"]; HHHuman *human_4 = [HHHuman humanWithName:@"xiaohao"]; HHHuman *human_5 = [HHHuman humanWithName:@"beijing"]; id list = @[human_1,human_2,human_3,human_4,human_5]; HHHumanRelationShip *relationShip = [HHHumanRelationShip relationShipWithHuman:human_0 family:list]; NSLog(@"%@",relationShip); return 0; }
NSHashTable
很明显,大家能够看到这样造成了程序代码的臃肿根据上述需求和功能,在iOS6之后,Objective-C Foundation框架中添加了两个类分别是
NSHashTable和
NSMapTable
NSHashTable构造函数
- (instancetype)initWithOptions:(NSPointerFunctionsOptions)options capacity:(NSUInteger)initialCapacity
- (instancetype)initWithPointerFunctions:(NSPointerFunctions *)functions capacity:(NSUInteger)initialCapacity
+ (NSHashTable *)hashTableWithOptions:(NSPointerFunctionsOptions)options;
+ (id)hashTableWithWeakObjects;
+ (NSHashTable *)weakObjectsHashTable;在创建
NSHashTable对象时,会传
NSPointerFunctionsOptions参数,列举如下:
NSHashTableStrongMemory将HashTable容器内的对象引用计数+1一次
NSHashTableZeroingWeakMemory在OSX 10.8之后已经废弃
NSHashTableCopyIn将添加到容器的对象通过
NSCopying中的方法,复制一个新的对象存入
HashTable容器
NSHashTableObjectPointerPersonality使用移位指针(shifted pointer)来做hash检测及确定两个对象是否相等;
NSHashTableWeakMemory不会修改HashTable容器内对象元素的引用计数,并且对象释放后,会被自动移除对于我们来说,
NSHashTable吸引力比较大的即
NSHashTableWeakMemory特性.使用一段代码来展示功能:
#import <Foundation/Foundation.h> @interface HHHuman : NSObject @property (nonatomic ,strong) NSString *name; @property (nonatomic ,strong) NSHashTable *family; + (instancetype) humanWithName:(NSString *)n; @end @implementation HHHuman + (instancetype) humanWithName:(NSString *)n { HHHuman *human = [[HHHuman alloc] init]; human.name = n; human.family = [NSHashTable hashTableWithOptions:NSHashTableWeakMemory]; [human.family addObject:human]; return [human autorelease]; } - (NSString *)description { return [NSString stringWithFormat:@"%@'s retainCount is %lu",self.name,[self retainCount]]; } - (void)dealloc { self.name = nil; self.family = nil; [super dealloc]; } @end int main(int argc, const char * argv[]) { //创建一个NSHashTableWeakMemory特性的HashTable对象 NSHashTable *hash_tab = [NSHashTable hashTableWithOptions:NSHashTableWeakMemory]; //创建自动释放池对象 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //通过便利构造器获取一个name属性是lewis的human对象 HHHuman *human = [HHHuman humanWithName:@"lewis"]; //将该对象添加到HashTable容器中 [hash_tab addObject:human]; //释放之前打印human NSLog(@"before pool:%@",human); //将自动释放池释放掉 [pool drain]; //释放之后打印hash_tab NSLog(@"after pool:%@",hash_tab); return 0; }在控制台输出的结果如下
before pool:lewis's retainCount is 1 after pool:NSHashTable { }我们可以看到,当
pool对象释放时,
human的引用计数会执行一次
-1,human对象在内存中就会自动释放,并且相应的
hash_tab对象中的对象也会被自动移除.而我们在创建
hash_tab时使用的是
NSHashTableStrongMemory特性话,那么控制台输出的结果如下:
before pool:lewis's retainCount is 2 after pool:NSHashTable { [13] lewis's retainCount is 1 }有了
NSHashTable就可以完成我们文章一开始的需求了.
#import <Foundation/Foundation.h> @interface HHHuman : NSObject @property (nonatomic ,strong) NSString *name; @property (nonatomic ,strong) NSHashTable *family; + (instancetype) humanWithName:(NSString *)n; @end @implementation HHHuman + (instancetype) humanWithName:(NSString *)n { HHHuman *human = [[HHHuman alloc] init]; human.name = n; human.family = [NSHashTable hashTableWithOptions:NSHashTableWeakMemory]; [human.family addObject:human]; return [human autorelease]; } - (NSString *)description { return [NSString stringWithFormat:@"%@'s retainCount is %lu",self.name,[self retainCount]]; } - (void)dealloc { self.name = nil; self.family = nil; [super dealloc]; } @end
NSHashTable可以使用的函数
typedef struct {NSUInteger _pi; NSUInteger _si; void *_bs;} NSHashEnumerator; FOUNDATION_EXPORT void NSFreeHashTable(NSHashTable *table); FOUNDATION_EXPORT void NSResetHashTable(NSHashTable *table); FOUNDATION_EXPORT BOOL NSCompareHashTables(NSHashTable *table1, NSHashTable *table2); FOUNDATION_EXPORT NSHashTable *NSCopyHashTableWithZone(NSHashTable *table, NSZone *zone); FOUNDATION_EXPORT void *NSHashGet(NSHashTable *table, const void *pointer); FOUNDATION_EXPORT void NSHashInsert(NSHashTable *table, const void *pointer); FOUNDATION_EXPORT void NSHashInsertKnownAbsent(NSHashTable *table, const void *pointer); FOUNDATION_EXPORT void *NSHashInsertIfAbsent(NSHashTable *table, const void *pointer); FOUNDATION_EXPORT void NSHashRemove(NSHashTable *table, const void *pointer); FOUNDATION_EXPORT NSHashEnumerator NSEnumerateHashTable(NSHashTable *table); FOUNDATION_EXPORT void *NSNextHashEnumeratorItem(NSHashEnumerator *enumerator); FOUNDATION_EXPORT void NSEndHashTableEnumeration(NSHashEnumerator *enumerator); FOUNDATION_EXPORT NSUInteger NSCountHashTable(NSHashTable *table); FOUNDATION_EXPORT NSString *NSStringFromHashTable(NSHashTable *table); FOUNDATION_EXPORT NSArray *NSAllHashTableObjects(NSHashTable *table);
NSMapTable
NSMapTable构造函数
- (instancetype)initWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions capacity:(NSUInteger)initialCapacity;
- (instancetype)initWithKeyPointerFunctions:(NSPointerFunctions *)keyFunctions valuePointerFunctions:(NSPointerFunctions *)valueFunctions capacity:(NSUInteger)initialCapacity;
+ (NSMapTable *)mapTableWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions;
+ (NSMapTable *)strongToStrongObjectsMapTable;
+ (NSMapTable *)weakToStrongObjectsMapTable;
+ (NSMapTable *)strongToWeakObjectsMapTable;
+ (NSMapTable *)weakToWeakObjectsMapTable;
NSMapTable对象类似与
NSDictionary的数据结构,但是
NSMapTable功能比
NSDictionary对象要多的功能就是可以设置
key和
value的NSPointerFunctionsOptions特性!其他的用法与
NSDictionary相同.
NSMapTable可以使用的函数
FOUNDATION_EXPORT void NSFreeMapTable(NSMapTable *table); FOUNDATION_EXPORT void NSResetMapTable(NSMapTable *table); FOUNDATION_EXPORT BOOL NSCompareMapTables(NSMapTable *table1, NSMapTable *table2); FOUNDATION_EXPORT NSMapTable *NSCopyMapTableWithZone(NSMapTable *table, NSZone *zone); FOUNDATION_EXPORT BOOL NSMapMember(NSMapTable *table, const void *key, void **originalKey, void **value); FOUNDATION_EXPORT void *NSMapGet(NSMapTable *table, const void *key); FOUNDATION_EXPORT void NSMapInsert(NSMapTable *table, const void *key, const void *value); FOUNDATION_EXPORT void NSMapInsertKnownAbsent(NSMapTable *table, const void *key, const void *value); FOUNDATION_EXPORT void *NSMapInsertIfAbsent(NSMapTable *table, const void *key, const void *value); FOUNDATION_EXPORT void NSMapRemove(NSMapTable *table, const void *key); FOUNDATION_EXPORT NSMapEnumerator NSEnumerateMapTable(NSMapTable *table); FOUNDATION_EXPORT BOOL NSNextMapEnumeratorPair(NSMapEnumerator *enumerator, void **key, void **value); FOUNDATION_EXPORT void NSEndMapTableEnumeration(NSMapEnumerator *enumerator); FOUNDATION_EXPORT NSUInteger NSCountMapTable(NSMapTable *table); FOUNDATION_EXPORT NSString *NSStringFromMapTable(NSMapTable *table); FOUNDATION_EXPORT NSArray *NSAllMapTableKeys(NSMapTable *table); FOUNDATION_EXPORT NSArray *NSAllMapTableValues(NSMapTable *table);
相关文章推荐
- 奴隶主谜题,何解?
- navigation的Title和TabBar的Title不同时怎么解决
- tomcat(10)安全性
- apache commons io Findbugs报告(2016-04-21更新)
- 十二、图的算法入门--(3)最小生成树---Kruskal算法实现
- Git命令家底儿及Git数据通信原理详解
- 关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
- thinkphp--空操作,空控制器的处理方法
- 【总结】网站性能分析-前台优化
- 北大CMA课程一次搞定CMA认证
- 跟我学分布式-cookie-session实现
- python内置模块string源码浅析之maketrans
- java web 生成图片验证码的功能实现
- Dialog点击屏幕其他地方消失的事件
- 单例模式的七种写法, 面试题:线程安全的单例模式
- jQuery Mobile orientationchange 事件
- saltstack 几个常用命令
- 怎么让您的应用进入google索引?
- 关于在textvie中提取电话号码,点击跳转拨打界面
- 柴俊理金:5月9日现货黄金,宁贵沥青操作策略