使用NSHashTable存储引用对象

来源:互联网 发布:sql怎么查联合主键 编辑:程序博客网 时间:2024/06/14 08:47

      我们使用集合(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表用来存储弱引用对象.

0 0