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. Python2.x 中文乱码问题

    Python 文件中如果未指定编码,在执行过程会出现报错: #!/usr/bin/pythonprint "你好,世界"; 以上程序执行输出结果为: File "test ...

  2. redis-springdata-api

    使用StringRedisTempalte操作redis五种数据类型 spring-data中继承了redisTemplate, 提供redis非切片连接池 代码github地址: https://g ...

  3. php的explode()和split()的区别

    都是分割,区别就是,split要用转移字符: 1.   $test = end(explode('.', 'abc.txt'));    echo $test;//output txt   2.    ...

  4. hadoop学习笔记(八):MapReduce

    一.MapReduce编程模型 一种分布式计算框架,解决海量数据的计算问题. MapReduce将整个并行计算过程抽象到两个函数: Map(映射):对一些独立元素组成的列表的每一个元素进行制定的操作, ...

  5. BIO、NIO和AIO的区别

    一:事件分离器 在IO读写时,把 IO请求 与 读写操作 分离调配进行,需要用到事件分离器.根据处理机制的不同,事件分离器又分为:同步的Reactor和异步的Proactor. Reactor模型: ...

  6. CSS starts

    I have not written any articles here since I graduated from my university. Now I begin to write down ...

  7. 涉及到【分页】的table的请求模式

    step:1 点击分页器的内容 trigger事件句柄 (pagination, filters, sorter) => {//或者(page, pageSize)等 this.props.on ...

  8. 今年新鲜出炉的30个流行Android库,你一定需要

    作者|Michal Bialas 2017年快过去了,你年初的定的目标都快完成了吗?总结过去三个月内发布的 最新的30 个 Android 库和项目.你一定需要,建议收藏!让你事半功倍 1.Mater ...

  9. Android 应用安装

    DDMS下Files Explorer /data/app/xxx.apk 安装过程:1.拷贝文件xxx.apk到/data/app/xxx-1.apk 2.在/data/data目录下创建一个文件夹 ...

  10. SqlSugarClientHelper

    using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using Sy ...