标签:
NSMapTable、NSHashTable与NSPointerArray的封装
说明
NSMapTable对应NSDictionary;NSHashTable对应NSSet;NSPointerArray对应NSArray,本人通过装饰设计模式对他们的使用进行了封装。
源码
https://github.com/YouXianMing/WeakList
// // WeakDictionary.h // IteratorPattern // // Created by YouXianMing on 15/9/12. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @interface WeakDictionary : NSObject /** * 元素个数 */ @property (readonly) NSUInteger count; /** * 获取对象 * * @param aKey * * @return 对象 */ - (id)objectForKey:(id)aKey; /** * 根据键值移除对象 * * @param aKey 键值 */ - (void)removeObjectForKey:(id)aKey; /** * 添加对象 * * @param anObject 对象 * @param aKey 键值 */ - (void)setObject:(id)anObject forKey:(id)aKey; /** * 键值枚举器 * * @return 枚举器 */ - (NSEnumerator *)keyEnumerator; /** * 对象枚举器 * * @return 对象枚举器 */ - (NSEnumerator *)objectEnumerator; /** * 移除所有对象 */ - (void)removeAllObjects; /** * 返回字典 * * @return 字典 */ - (NSDictionary *)dictionaryRepresentation; @end
// // WeakDictionary.m // IteratorPattern // // Created by YouXianMing on 15/9/12. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "WeakDictionary.h" @interface WeakDictionary () { NSMapTable *_mapTable; } @end @implementation WeakDictionary - (instancetype)init { self = [super init]; if (self) { _mapTable = [NSMapTable strongToWeakObjectsMapTable]; } return self; } - (id)objectForKey:(id)aKey { return [_mapTable objectForKey:aKey]; } - (void)removeObjectForKey:(id)aKey { [_mapTable removeObjectForKey:aKey]; } - (void)setObject:(id)anObject forKey:(id)aKey { [_mapTable setObject:anObject forKey:aKey]; } - (NSEnumerator *)keyEnumerator { return [_mapTable keyEnumerator]; } - (NSEnumerator *)objectEnumerator { return [_mapTable objectEnumerator]; } - (void)removeAllObjects { [_mapTable removeAllObjects]; } - (NSDictionary *)dictionaryRepresentation { return [_mapTable dictionaryRepresentation]; } @synthesize count = _count; - (NSUInteger)count { return _mapTable.count; } - (NSString *)description { return [NSString stringWithFormat:@"%@", _mapTable.dictionaryRepresentation]; } @end
// // WeakSet.h // IteratorPattern // // Created by YouXianMing on 15/9/12. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @interface WeakSet : NSObject /** * 元素个数 */ @property (readonly) NSUInteger count; /** * 所有对象 */ @property (readonly, copy) NSArray *allObjects; /** * 获取一个对象 */ @property (readonly, nonatomic) id anyObject; /** * 获取集合 */ @property (readonly, copy) NSSet *setRepresentation; - (id)member:(id)object; - (NSEnumerator *)objectEnumerator; - (void)addObject:(id)object; - (void)removeObject:(id)object; - (void)removeAllObjects; - (BOOL)containsObject:(id)anObject; @end
// // WeakSet.m // IteratorPattern // // Created by YouXianMing on 15/9/12. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "WeakSet.h" @interface WeakSet () { NSHashTable *_hashTable; } @end @implementation WeakSet - (instancetype)init { self = [super init]; if (self) { _hashTable = [NSHashTable weakObjectsHashTable]; } return self; } - (id)member:(id)object { return [_hashTable member:object]; } - (NSEnumerator *)objectEnumerator { return [_hashTable objectEnumerator]; } - (void)addObject:(id)object { [_hashTable addObject:object]; } - (void)removeObject:(id)object { [_hashTable removeObject:object]; } - (void)removeAllObjects { [_hashTable removeAllObjects]; } - (BOOL)containsObject:(id)anObject { return [_hashTable containsObject:anObject]; } @synthesize count = _count; - (NSUInteger)count { return _hashTable.count; } @synthesize allObjects = _allObjects; - (NSArray *)allObjects { return [_hashTable allObjects]; } @synthesize anyObject = _anyObject; - (id)anyObject { return [_hashTable anyObject]; } @synthesize setRepresentation = _setRepresentation; - (NSSet *)setRepresentation { return [_hashTable setRepresentation]; } - (NSString *)description { return [NSString stringWithFormat:@"%@", _hashTable.allObjects]; } @end
// // WeakArray.h // IteratorPattern // // Created by YouXianMing on 15/9/12. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @interface WeakArray : NSObject @property (readonly, copy) NSArray *allObjects; @property (readonly) NSUInteger count; - (id)objectAtIndex:(NSUInteger)index; - (void)addObject:(id)object; - (void)removeObjectAtIndex:(NSUInteger)index; - (void)insertObject:(id)object atIndex:(NSUInteger)index; - (void)replaceObjectAtIndex:(NSUInteger)index withPointer:(id)object; - (void)compact; @end
// // WeakArray.m // IteratorPattern // // Created by YouXianMing on 15/9/12. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "WeakArray.h" @interface WeakArray () { NSPointerArray *_pointerArray; } @end @implementation WeakArray - (instancetype)init { self = [super init]; if (self) { _pointerArray = [NSPointerArray pointerArrayWithOptions:NSPointerFunctionsWeakMemory]; } return self; } - (id)objectAtIndex:(NSUInteger)index { return [_pointerArray pointerAtIndex:index]; } - (void)addObject:(id)object { [_pointerArray addPointer:(__bridge void *)(object)]; } - (void)removeObjectAtIndex:(NSUInteger)index { [_pointerArray removePointerAtIndex:index]; } - (void)insertObject:(id)object atIndex:(NSUInteger)index { [_pointerArray insertPointer:(__bridge void *)(object) atIndex:index]; } - (void)replaceObjectAtIndex:(NSUInteger)index withPointer:(id)object { [_pointerArray replacePointerAtIndex:index withPointer:(__bridge void *)(object)]; } - (void)compact { [_pointerArray compact]; } @synthesize count = _count; - (NSUInteger)count { return _pointerArray.count; } - (NSString *)description { return [NSString stringWithFormat:@"%@", _pointerArray.allObjects]; } @synthesize allObjects = _allObjects; - (NSArray *)allObjects { return _pointerArray.allObjects; } @end
使用
NSMapTable、NSHashTable与NSPointerArray的封装
标签:
原文地址:http://www.cnblogs.com/YouXianMing/p/4803290.html