一個專案中的需求
在iOS專案開發過程中,我們經常會使用到NSSet
、NSArray
、NSDictionary
三個類,它們為我們設計較友好的資料結構時提供了很方便的方法
先準備本文中將要使用的物件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#import @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
;
1 2 3 4 5 6 7 8 |
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); |
輸出的結果如下:
1 2 3 4 5 6 7 |
( "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
;
1 2 3 4 5 6 7 8 |
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); |
輸出的結果如下:
1 2 3 4 5 6 7 |
( "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
這樣就會有可能發生迴圈持有的問題,例如如下程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
@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
,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#import @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
特性.
使用一段程式碼來展示功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#import @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; } |
在控制檯輸出的結果如下
1 2 3 |
before pool:lewis's retainCount is 1 after pool:NSHashTable { } |
我們可以看到,當pool
物件釋放時,human
的引用計數會執行一次-1
,human物件在記憶體中就會自動釋放,並且相應的hash_tab
物件中的物件也會被自動移除.
而我們在建立hash_tab
時使用的是NSHashTableStrongMemory
特性話,那麼控制檯輸出的結果如下:
1 2 3 4 |
before pool:lewis's retainCount is 2 after pool:NSHashTable { [13] lewis's retainCount is 1 } |
有了NSHashTable
就可以完成我們文章一開始的需求了.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#import @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可以使用的函式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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可以使用的函式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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); |