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. 机器学习--boosting家族之GBDT

    本文就对Boosting家族中另一个重要的算法梯度提升树(Gradient Boosting Decison Tree, 以下简称GBDT)做一个总结.GBDT有很多简称,有GBT(Gradient ...

  2. [C语言]声明解析器cdecl修改版

    一.写在前面 K&R曾经在书中承认,"C语言声明的语法有时会带来严重的问题.".由于历史原因(BCPL语言只有唯一一个类型——二进制字),C语言声明的语法在各种合理的组合下 ...

  3. centos7-windows10 双系统安装

    win10默认, 然后压缩出来一个卷安装win7: http://www.techweb.com.cn/network/system/2016-12-21/2456741.shtml http://b ...

  4. Java多态的一些陷阱

    Java多态是如何实现的? Java的多态和C++一样,是通过延时绑定(late binding)或者说运行时绑定(runtime binding)来实现的.当调用某一个对象引用的方法时,因为编译器并 ...

  5. Storm:分布式流式计算框架

    Storm是一个分布式的.高容错的实时计算系统.Storm适用的场景: Storm可以用来用来处理源源不断的消息,并将处理之后的结果保存到持久化介质中. 由于Storm的处理组件都是分布式的,而且处理 ...

  6. node.js获取url中的各个参数

    实例代码test.js var http=require('http'); var url=require('url'); var querystring=require('querystring') ...

  7. 生成类似于MongoDB产生的ObjectId

    package com.jt.boot.utils; import com.google.common.base.Objects; import java.net.NetworkInterface; ...

  8. linq之多表连接

    1.左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals ...

  9. 南阳nyoj 56 阶乘因式分解(一)

    阶乘因式分解(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 给定两个数m,n,其中m是一个素数. 将n(0<=n<=10000)的阶乘分解质因数, ...

  10. hdu 1978 How many ways 记忆化搜索 经典例题

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...