一个项目中的需求
在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);