https://github.com/YouXianMing

使用NSHashTable存储引用对象

NSHashTable

      我们使用集合(NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSSet,NSMutableSet)存储对象时会对其强引用(你可以使用单例的集合存储数据试一下咯),有时候我们不想这样子,怎么办呢?

 

      那就使用NSHashTable这个集合吧,它的使用方法与NSSet完全相似,不同的是,它的一种初始化方式是weakObjectsHashTable,专门用来存储弱引用对象,不会持有它,那个对象的所有人消失了,这个对象也会从这个集合中消失,多么人性化!

 

      下面介绍它的一些方法:

 

- (BOOL)containsObject:(id)anObject

Returns a Boolean value that indicates whether the hash table contains a given object.

返回一个bool值,用来指示这个hash表中是否包括了你给与的对象.

 

- (void)addObject:(id)object

Adds a given object to the hash table.

将一个对象添加进hash表中.

 

- (void)removeObject:(id)object

Removes a given object from the hash table.

从hash表中移除你给定的对象.

 

+ (id)weakObjectsHashTable

Returns a new hash table for storing weak references to its contents.

返回一个hash表用来存储弱引用对象.

 

 

posted @ 2014-04-24 16:43  YouXianMing  阅读(1199)  评论(0编辑  收藏  举报