[转][iOS]NSHashTable & NSMapTable
NSSet and NSDictionary, along with NSArray are the workhorse collection classes of Foundation. Unlike other standard libraries, implementation details are hidden from developers, allowing them to write simple code and trust that it will be (reasonably) performant.
However, even the best abstractions break down; their underlying assumptions overturned. In these cases, developers either venture further down the abstraction, or, if available use a more general-purpose solution.
For NSSet and NSDictionary, the breaking assumption was in the memory behavior when storing objects in the collection. For NSSet, objects are a strongly referenced, as are NSDictionary values. Keys, on the other hand, are copied by NSDictionary. If a developer wanted to store a weak value, or use a non-<NSCopying>-conforming object as a key, they could be clever and use NSValue +valueWithNonretainedObject. Or, as of iOS 6 (and as far back as OS X Leopard), they could use NSHashTable or NSMapTable, the more general-case counterparts to NSSet or NSDictionary, respectively.
So without further ado, here's everything you need to know about two of the more obscure members of Foundation's collection classes:
NSHashTable
NSHashTable is a general-purpose analogue of NSSet. Contrasted with the behavior of NSSet / NSMutableSet, NSHashTable has the following characteristics:
NSSet/NSMutableSetholdsstrongreferences to members, which are tested for hashing and equality using the methodshashandisEqual:.NSHashTableis mutable, without an immutable counterpart.NSHashTablecan holdweakreferences to its members.NSHashTablecancopymembers on input.NSHashTablecan contain arbitrary pointers, and use pointer identity for equality and hashing checks.
Usage
NSHashTable *hashTable = [NSHashTable hashTableWithOptions:NSPointerFunctionsCopyIn];
[hashTable addObject:@"foo"];
[hashTable addObject:@"bar"];
[hashTable addObject:@42];
[hashTable removeObject:@"bar"];
NSLog(@"Members: %@", [hashTable allObjects]);
NSHashTable objects are initialized with an option for any of the following behaviors. Deprecated enum values are due to NSHashTable being ported from Garbage-Collected OS X to ARC-ified iOS. Other values are aliased to options defined by NSPointerFunctions, which will be covered next week on NSHipster.
NSHashTableStrongMemory: Equal toNSPointerFunctionsStrongMemory. This is the default behavior, equivalent toNSSetmember storage.NSHashTableWeakMemory: Equal toNSPointerFunctionsWeakMemory. Uses weak read and write barriers. UsingNSPointerFunctionsWeakMemory, object references will turn toNULLon last release.NSHashTableZeroingWeakMemory: This option has been deprecated. Instead use theNSHashTableWeakMemoryoption.NSHashTableCopyIn: Use the memory acquire function to allocate and copy items on input (seeNSPointerFunction -acquireFunction). Equal toNSPointerFunctionsCopyIn.NSHashTableObjectPointerPersonality: Use shifted pointer for the hash value and direct comparison to determine equality; use the description method for a description. Equal toNSPointerFunctionsObjectPointerPersonality.
NSMapTable
NSMapTable is a general-purpose analogue of NSDictionary. Contrasted with the behavior of NSDictionary / NSMutableDictionary, NSMapTable has the following characteristics:
NSDictionary/NSMutableDictionarycopies keys, and holds strong references to values.NSMapTableis mutable, without an immutable counterpart.NSMapTablecan hold keys and values withweakreferences, in such a way that entries are removed when either the key or value is deallocated.NSMapTablecancopyits values on input.NSMapTablecan contain arbitrary pointers, and use pointer identity for equality and hashing checks.
Usage
Instances where one might use NSMapTable include non-copyable keys and storing weak references to keyed delegates or another kind of weak object.
id delegate = ...;
NSMapTable *mapTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
valueOptions:NSMapTableWeakMemory];
[mapTable setObject:delegate forKey:@"foo"];
NSLog(@"Keys: %@", [[mapTable keyEnumerator] allObjects]);
NSMapTable objects are initialized with options specifying behavior for both keys and values, using the following enum values:
NSMapTableStrongMemory: Specifies a strong reference from the map table to its contents.NSMapTableWeakMemory: Uses weak read and write barriers appropriate for ARC or GC. UsingNSPointerFunctionsWeakMemory, object references will turn toNULLon last release. Equal toNSMapTableZeroingWeakMemory.NSHashTableZeroingWeakMemory: This option has been superseded by theNSMapTableWeakMemoryoption.NSMapTableCopyIn: Use the memory acquire function to allocate and copy items on input (see acquireFunction (seeNSPointerFunction -acquireFunction). Equal to NSPointerFunctionsCopyIn.NSMapTableObjectPointerPersonality: Use shifted pointer hash and direct equality, object description. Equal toNSPointerFunctionsObjectPointerPersonality.
Subscripting
NSMapTable doesn't implement object subscripting, but it can be trivially added in a category. NSDictionary's NSCopying requirement for keys belongs to NSDictionary alone:
@implementation NSMapTable (NSHipsterSubscripting)
- (id)objectForKeyedSubscript:(id)key
{
return [self objectForKey:key];
}
- (void)setObject:(id)obj forKeyedSubscript:(id)key
{
[self setObject:obj forKey:key];
}
@end
As always, it's important to remember that programming is not about being clever: always approach a problem from the highest viable level of abstraction. NSSet and NSDictionary are great classes. For 99% of problems, they are undoubtedly the correct tool for the job. If, however, your problem has any of the particular memory management constraints described above, then NSHashTable & NSMapTablemay be worth a look.
---------
作为delegate弱引用的集合再合适不过了。
[转][iOS]NSHashTable & NSMapTable的更多相关文章
- mysqldump:Couldn't execute 'show create table `tablename`': Table tablename' doesn't exist (1146)
遇到了一个错误mysqldump: Couldn't execute 'show create table `CONCURRENCY_ERRORS`': Table INVOICE_OLD.CONCU ...
- ORA-01747: user.table.column, table.column 或列说明无效
Oracle.DataAccess.Client.OracleException ORA-01747: user.table.column, table.column 或列说明无效 原因1: 查了一下 ...
- SQLServer temporary table and table variable
Temporary tables are created in tempdb. The name "temporary" is slightly misleading, for ...
- Oracle10g 回收站及彻底删除table : drop table xx purge
drop后的表被放在回收站(user_recyclebin)里,而不是直接删除掉.这样,回收站里的表信息就可以被恢复,或彻底清除. 1.通过查询回收站user_recyclebin获取被删除的表信息, ...
- user.table.column, table.column 或列说明无效
Oracle统计采用别名出错(user.table.column, table.column 或列说明无效) >>>>>>>>>>>& ...
- lua中打印所以类型功能实现table嵌套table
lua中打印所以类型功能实现 本人測试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码例如以下 cclog = func ...
- 使用vue的v-for生成table , 给table加上序号
现在有一个使用mybatis的分页插件生成的table,table中数据是通过vue获得的 , 前台显示使用<tr v-for="item in items"> 后台v ...
- 表优化 altering table OPTIMIZE TABLE `sta_addr_copy`
表优化 altering table OPTIMIZE TABLE `sta_addr_copy` [总结] 1.实际测试的结果是,在state sqlaltering table OPTIMIZE ...
- MySQL check table/optimize table/analyze table/REPAIR TABLE
MySQL check table/optimize table/analyze table/REPAIR TABLE 转自:https://www.cnblogs.com/datastack/p/3 ...
随机推荐
- 企业应用开发模式 ERP项目中应用到的技术和工具
一.基础技术选型 C# .NET 3.5/4.0 这两个版本的.NET已经相当方便(Linq, Lambda,Parallel),语法简洁,配合WCF和WF两项技术,可以满足快速开发,维护方便的目标 ...
- 锋利的jQuery--jQuery与DOM对象的互相转换,DOM的三种操作(读书笔记一)
1.jQuery对象就是通过jQuery包装DOM对象后产生的对象. 2.jQuery对象和DOM对象的相互转换. 良好的书写风格: var $input=$("input" ...
- 《JS设计模式笔记》构造函数和工厂模式创建对象
工厂模式 function createPerson (name,age,job) { var o=new Object(); o.name=name; o.age=age; o.job=job; o ...
- 了解HTML列表
前面的话 从某种意义上讲,不是描述性文本的任何内容都可以认为是列表.人口普查.太阳系.餐馆菜单等都可以表示为一个列表或列表的列表.列表分为无序列表.有序列表和定义列表三种 无序列表 无序列表(unor ...
- php后台增删改跳转
php登录页面: <h1>登录界面</h1> <form action="dengluchuli.php" method="post&quo ...
- PHP实现全排列(递归算法)
算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为: ① 如果n=1,则排列P只有一 ...
- FFmpeg学习4:音频格式转换
前段时间,在学习试用FFmpeg播放音频的时候总是有杂音,网上的很多教程是基于之前版本的FFmpeg的,而新的FFmepg3中audio增加了平面(planar)格式,而SDL播放音频是不支持平面格式 ...
- 【十大经典数据挖掘算法】PageRank
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 我特地把PageRank作为[十大经 ...
- [C1] C1FlexGrid 行列增删&单元格合并拆分
上一篇中实现了 C1FlexGrid的撤销还原功能,这篇是要仿 Excel 做一个行列删除以及单元格的自由合并拆分,楼主怕在原工程里复杂的说不清道不明,所以干脆提取出来做了一个 Demo 来说明实现过 ...
- Could not find a suitable SDK to target
安装windows-10-sdk 应该可解决此问题 windows-10-sdk下载地址: developer.microsoft.com/en-us/windows/downloads/wi ...