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的封装的更多相关文章

  1. Cocoa 集合类型:NSPointerArray,NSMapTable,NSHashTable

    iOS 中有很多种集合类型,最为常见的可能就 NSArray.NSDictionary.NSSet,但其实还有 NSPointerArray.NSMapTable.NSHashTable 等类型,虽然 ...

  2. iOS 开发知识小集(1)

    iOS 开发知识小集(1) 2015-05-15  iOS大全 (点击上方蓝字,快速关注我们) 一直想做这样一个小册子,来记录自己平时开发.阅读博客.看书.代码分析和与人交流中遇到的各种问题.之前有过 ...

  3. NSHashTable NSPointerArray

    NSHashTable和NSMapTable能够对持有的对象做strong或weak存储,弱持有weak引用对象,当weak对象释放后会自动从表中移除     http://blog.csdn.net ...

  4. NSHashtable and NSMaptable

    本文转自Nidom的博客,原文:<NSHashtable & NSMaptable>   NSSet, NSDictionary, NSArray是Foundation框架关于集合 ...

  5. ios NSHashTable & NSMapTable

    在ios开发中大家用到更多的集合类可能是像NSSet或者NSDictionary,NSArray这样的.这里要介绍的是更少人使用的两个类,一个是NSMapTable,另一个是NSHashTable. ...

  6. 【转】NSHashtable and NSMaptable

    本文转自Nidom的博客,原文:<NSHashtable & NSMaptable>   NSSet, NSDictionary, NSArray是Foundation框架关于集合 ...

  7. NSHashTable 和 NSMapTable学习

    今天,在实现play gif时间功能,我看见两个陌生班,只需看看这个纪录: NSSet和NSDictionary是两个经常使用的类,可是他们默认假定了当中对象的内存行为.对于NSSet.object是 ...

  8. 封装NSMapTable并简易的使用

    封装NSMapTable并简易的使用 NSMapTable是弱引用的字典,可以用来存储对象,该对象消失了也没有关系,对于控制器越级跳转相当有用:) WeakDictionary.h 与 WeakDic ...

  9. NSDictionary和NSMaptable, NSArray,NSSet,NSOrderedSet和NSHashTable的区别

    NSSet, NSDictionary, NSArray是Foundation框架关于集合操作的常用类, 和其他标准的集合操作库不同, 他们的实现方法对开发者进行隐藏, 只允许开发者写一些简单的代码, ...

随机推荐

  1. 17.async 函数

    async 函数 async 函数 含义 ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖. 前文有一个 ...

  2. ORDER BY 高级用法之CASE WHEN

    今天在公司查看一段sql SP代码,发现了一段比较奇怪的代码. 大概长这样子: Select * from tableA ORDER BY ColA , CASE END 小弟才疏学浅,咋一看到代码以 ...

  3. 关于git远程版本库的一些问题之解决

    Part1:CentOS6.5免密码登录 修改/etc/ssh/sshd_config RSAAuthentication yesPubkeyAuthentication yesAuthorizedK ...

  4. 【转】ArrayBlockingQueue浅析

    ArrayBlockingQueue是常用的线程集合,在线程池中也常常被当做任务队列来使用.使用频率特别高.他是维护的是一个循环队列(基于数组实现),循环结构在数据结构中比较常见,但是在源码实现中还是 ...

  5. linux命令更改服务器时间

    1. linux更改服务器时间: 权限:root用户才有权限更改服务器时间 使用date命令即可设置系统时间. 2. 查看系统时间 date 3. 设置当前系统时间为2015年5月8日19点48分0秒 ...

  6. jQuery基础(样式篇,DOM对象,选择器,属性样式)

      1. $(document).ready 的作用是等页面的文档(document)中的节点都加载完毕后,再执行后续的代码,因为我们在执行代码的时候,可能会依赖页面的某一个元素,我们要确保这个元素真 ...

  7. html5 分组标签 br hr p div blockquote figure ul ol li pre

    <br>    换行, 单标签 <hr>    分割线,水平线 <p>    段落, 有<br>换行功能, 而且行距会比普通换行要宽. <div& ...

  8. 在已配置成功的opencv3.2.0下配置opencv_contrib模块

    简介: 之前在Ubuntu下配置OpenCV时,因为对opencv3..0不是特别了解,没有把opencv_contrib进行安装,这里提醒大家尽量要一次性安装完毕,减少不必要的麻烦. .0文件夹 ( ...

  9. View的setTag和getTag方法

    ---恢复内容开始--- public View getView(int position, View convertView, ViewGroup parent) { Msg msg =getIte ...

  10. Flutter与Android混合开发及Platform Channel的使用

    相对于单独开发Flutter应用,混合开发对于线上项目更具有实际意义,可以把风险控制到最低,也可以进行实战上线.所以介绍 集成已有项目 混合开发涉及原生Native和Flutter进行通信传输,还有插 ...