[转][iOS]NSHash?Table & NSMap?Table

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 / NSMutableSetNSHashTable has the following characteristics:

  • NSSet / NSMutableSet holds strong references to members, which are tested for hashing and equality using the methods hash and isEqual:.
  • NSHashTable is mutable, without an immutable counterpart.
  • NSHashTable can hold weak references to its members.
  • NSHashTable can copy members on input.
  • NSHashTable can 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 to NSPointerFunctionsStrongMemory. This is the default behavior, equivalent to NSSet member storage.
  • NSHashTableWeakMemory: Equal to NSPointerFunctionsWeakMemory. Uses weak read and write barriers. Using NSPointerFunctionsWeakMemory, object references will turn to NULL on last release.
  • NSHashTableZeroingWeakMemory: This option has been deprecated. Instead use the NSHashTableWeakMemory option.
  • NSHashTableCopyIn: Use the memory acquire function to allocate and copy items on input (see NSPointerFunction -acquireFunction). Equal to NSPointerFunctionsCopyIn.
  • NSHashTableObjectPointerPersonality: Use shifted pointer for the hash value and direct comparison to determine equality; use the description method for a description. Equal to NSPointerFunctionsObjectPointerPersonality.

NSMapTable

NSMapTable is a general-purpose analogue of NSDictionary. Contrasted with the behavior of NSDictionary / NSMutableDictionaryNSMapTable has the following characteristics:

  • NSDictionary / NSMutableDictionary copies keys, and holds strong references to values.
  • NSMapTable is mutable, without an immutable counterpart.
  • NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.
  • NSMapTable can copy its values on input.
  • NSMapTable can 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. Using NSPointerFunctionsWeakMemory, object references will turn to NULL on last release. Equal to NSMapTableZeroingWeakMemory.
  • NSHashTableZeroingWeakMemory: This option has been superseded by the NSMapTableWeakMemory option.
  • NSMapTableCopyIn: Use the memory acquire function to allocate and copy items on input (see acquireFunction (see NSPointerFunction -acquireFunction). Equal to NSPointerFunctionsCopyIn.
  • NSMapTableObjectPointerPersonality: Use shifted pointer hash and direct equality, object description. Equal to NSPointerFunctionsObjectPointerPersonality.

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弱引用的集合再合适不过了。

转自:http://nshipster.com/nshashtable-and-nsmaptable/

时间: 2024-10-26 01:59:06

[转][iOS]NSHash?Table & NSMap?Table的相关文章

lua中打印所以类型功能实现table嵌套table

lua中打印所以类型功能实现 本人测试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码如下 cclog = function( ... ) local tv = "\n" local xn = 0 local function tvlinet(xn) -- body for i=1,xn do tv = tv.."\t" end end local function printTab(i

Hive - Create Table&amp;Drop Table &amp; ALTER Table(中)

译注:书接上篇,了解过创建表以及load data后,假如发现需要更改表字段类型或者添加表字段,怎么办?这篇文章将进一步了解具体细节. This chapter explains how to alter the attributes of a table such as changing its table name, changing column names, adding columns, and deleting or replacing columns. Alter Table St

can&#39;t initialize iptables table `nat&#39;: Table does not exist

第一种 如果说nat名字是大写的NAT 试试iptables -tnat -L 原因是如果使用命令iptables -tNAT -L 是没有NAT表的也会出现这个错误 第二种:该错误完整应该是这样的: can't initialize iptables table `nat': Table does not exist (do you need to insmod?) Perhaps iptables or your kernel needs to be upgraded. 首先 [[email

“ORA-01747: user.table.column, table.column 或列说明无效” 的解决方案

此问题的原因是因为表的列名称使用了Oracle声明的关键字,列名起的不好引起的. 如果列很多,又不好确定是哪个列名使用了关键字,以下建议可供参考: select * from v$reserved_words where keyword in( select COLUMN_NAME from all_tab_columns where table_name = 'HP_FFS' and owner='用户名大写' ); 除了与oracle关键字段冲突错误外(select * from v$res

A MySQL foreign keys drop table, re-create table example

Summary: How to drop MySQL database tables and recreate them when you have foreign keyrelationships between the tables. This is pretty obscure, but I thought I'd post it here so I wouldn't forget how to do this ... if you ever have a situation when u

Oracle10g 回收站及彻底删除table : drop table xx purge

drop后的表被放在回收站(user_recyclebin)里,而不是直接删除掉.这样,回收站里的表信息就可以被恢复,或彻底清除. 1.通过查询回收站user_recyclebin获取被删除的表信息,然后使用语句flashback table <user_recyclebin.object_name or user_recyclebin.original_name> to before drop [rename to <new_table_name>]; 将回收站里的表恢复为原名称

配合bootstrap实现的table 嵌套table

不要忘了引入bootstrap.css库 html部分 <div class="container"> <div class="row"> <div class="col-sm-10"> <table class="table table-bordered"> <thead style="background: #ccc;"> <tr>

ORA-01747: user.table.column, table.column 或列说明无效

Oracle.DataAccess.Client.OracleException ORA-01747: user.table.column, table.column 或列说明无效 原因1: 查了一下是由于Oracle 数据库列名起的不好引起的,名字用到了数据库的关键字. 如果列很多,又不好确定是哪个列名使用了关键字,以下建议可供参考: 我用以下方法定位 select * from v$reserved_words where keyword in( select COLUMN_NAME fro

iOS开发&gt;学无止境 - 加速Table Views开发的10个建议

英文:David McGraw 译者:CocoaChina译者yake_099 网址:http://www.cocoachina.com/ios/20150729/12795.html 点击“阅读原文”可查看本文网页版 在我们开始之前,我准备从今年开始多听取一个意见.请花一些时间通过这篇简短的调查给我们一些反馈.这将会帮助我来帮助你. 如果你曾经跟collectionview打过交道,你可能已经意识到了这篇文章的价值.如果你没有注意速度这将会是一个大问题,你的用户会让你了解的.当你的scroll