NSMapTable、NSHashTable与NSPointerArray的封装
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的封装的更多相关文章
- Cocoa 集合类型:NSPointerArray,NSMapTable,NSHashTable
iOS 中有很多种集合类型,最为常见的可能就 NSArray.NSDictionary.NSSet,但其实还有 NSPointerArray.NSMapTable.NSHashTable 等类型,虽然 ...
- iOS 开发知识小集(1)
iOS 开发知识小集(1) 2015-05-15 iOS大全 (点击上方蓝字,快速关注我们) 一直想做这样一个小册子,来记录自己平时开发.阅读博客.看书.代码分析和与人交流中遇到的各种问题.之前有过 ...
- NSHashTable NSPointerArray
NSHashTable和NSMapTable能够对持有的对象做strong或weak存储,弱持有weak引用对象,当weak对象释放后会自动从表中移除 http://blog.csdn.net ...
- NSHashtable and NSMaptable
本文转自Nidom的博客,原文:<NSHashtable & NSMaptable> NSSet, NSDictionary, NSArray是Foundation框架关于集合 ...
- ios NSHashTable & NSMapTable
在ios开发中大家用到更多的集合类可能是像NSSet或者NSDictionary,NSArray这样的.这里要介绍的是更少人使用的两个类,一个是NSMapTable,另一个是NSHashTable. ...
- 【转】NSHashtable and NSMaptable
本文转自Nidom的博客,原文:<NSHashtable & NSMaptable> NSSet, NSDictionary, NSArray是Foundation框架关于集合 ...
- NSHashTable 和 NSMapTable学习
今天,在实现play gif时间功能,我看见两个陌生班,只需看看这个纪录: NSSet和NSDictionary是两个经常使用的类,可是他们默认假定了当中对象的内存行为.对于NSSet.object是 ...
- 封装NSMapTable并简易的使用
封装NSMapTable并简易的使用 NSMapTable是弱引用的字典,可以用来存储对象,该对象消失了也没有关系,对于控制器越级跳转相当有用:) WeakDictionary.h 与 WeakDic ...
- NSDictionary和NSMaptable, NSArray,NSSet,NSOrderedSet和NSHashTable的区别
NSSet, NSDictionary, NSArray是Foundation框架关于集合操作的常用类, 和其他标准的集合操作库不同, 他们的实现方法对开发者进行隐藏, 只允许开发者写一些简单的代码, ...
随机推荐
- 用python写一个爬虫——爬取性感小姐姐
忍着鼻血写代码 今天写一个简单的网上爬虫,爬取一个叫妹子图的网站里面所有妹子的图片. 然后试着先爬取了三页,大概有七百多张图片吧!各个诱人的很,有兴趣的同学可以一起来爬一下,大佬级程序员勿喷,简单爬虫 ...
- 使用Xshell和Xftfp部署简单的项目
最近本人偶尔接触到该如何部署项目,朋友要求截图,趁此之际,简单总结一下,以供大家分享,更希望各位大神指点,大家相互学习,有问题的勿喷. 1.使用环境:win 7+MyEclipse 2014 + to ...
- Java8常用新特性实践
前言: 时下Oracle开速迭代的Java社区以即将推出Java10,但尴尬的是不少小中企业仍使用JDK7甚至JDK6开发. 从上面列出的JDK8特性中我们可以发现Java8的部分特性很明显的是从Sc ...
- ASP.NET MVC4应用程序无法建立控制器的解决方案/获取自己需要的EF版本
具体错误是我建立控制器的时候出现如下图那样的错误: Unable to cast object of type 'System.Data.Entity.Core.Objects.ObjectConte ...
- 【转】通过CountDownLatch提升请求处理速度
countdownlatch是java多线程包concurrent里的一个常见工具类,通过使用它可以借助线程能力极大提升处理响应速度,且实现方式非常优雅.今天我们用一个实际案例和大家来讲解一下如何使用 ...
- java实现黑客帝国数字雨特效(转)
原文出处https://www.cnblogs.com/x110/p/4239585.html 我在原文的基础上做了优化,使动画看起来更流畅,效果如下 import java.awt.*; impor ...
- Tomcat启动项目两次
网上一搜,给出的答案都一样,不外乎:1.删除 Host 标签配置的 appBase="webapps"2.删除 Context 配置 此处这样做:重新添加Tomcat,选择好自己的 ...
- Differences between write through and write back
https://stackoverflow.com/questions/27087912/write-back-vs-write-through
- spring cloud 服务发现
Eureka 当注册中心使用. 注: 1.当仅有一台Eureka时,不需要向别的节点注册. 2.集群的时候,需要相互注册. 工作方式: 前提: Eureka //注册中心 provide1 / ...
- js-NodeList对象和HTMLCollection对象
getElementsByName()和getElementsByTagName()都返回NodeList对象,而类似document.images和document.forms的属性为HTMLCol ...