objective-c 可可中的哈希表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/474316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:03:54  来源:igfitidea点击:

HashTables in Cocoa

objective-ccocoamacoshashtable

提问by Ryan Delucchi

HashTables/HashMaps are one of the most (if not themost) useful of data-structures in existence. As such, one of the first things I investigated when starting to learn programming in Cocoa was how to create, populate, and read data from a hashtable.

散列表/包含HashMap是一个最(如果不是大多数)存在数据结构是有用的。因此,在开始学习 Cocoa 编程时,我研究的第一件事就是如何创建、填充和读取哈希表中的数据。

To my surprise: all the documentation I've been reading on Cocoa/Objective-C programming doesn't seem to explain this much at all. As a Java developer that uses "java.util" as if it were a bodily function: I am utterly baffled by this.

令我惊讶的是:我一直在阅读的关于 Cocoa/Objective-C 编程的所有文档似乎都没有解释这么多。作为使用“java.util”的 Java 开发人员,就好像它是一个身体功能一样:我对此完全感到困惑。

So, if someone could provide me with a primer for creating, populating, and reading the contents of a hashtable: I would greatly appreciate it.

因此,如果有人可以为我提供创建、填充和读取哈希表内容的入门指南:我将不胜感激。

回答by Martin Gordon

NSDictionaryand NSMutableDictionary?

NSDictionaryNSMutableDictionary?

And here's a simple example:

这是一个简单的例子:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:anObj forKey:@"foo"];
[dictionary objectForKey:@"foo"];
[dictionary removeObjectForKey:@"foo"];
[dictionary release];

回答by Julius Guzy

You can try using an NSHashTable!

您可以尝试使用NSHashTable

回答by Barry Wark

If you're using Leopard (and Cocoa's new Garbage Collection), you also want to take a look at NSMapTable.

如果您正在使用 Leopard(和 Cocoa 的新垃圾收集),您还想看看NSMapTable

回答by Chris Hanson

In addition to NSDictionary, also check out NSSet for when you need a collection with no order and no duplicates.

除了 NSDictionary 之外,当您需要一个没有顺序且没有重复的集合时,还可以查看 NSSet。

回答by mylittleswift

Use NSHashTablefrom iOS 6.0+ SDK. The hash table is modeled after NSSet with the following differences: It can hold weak references to its members. Its members may be copied on input or may use pointer identity for equality and hashing. It can contain arbitrary pointers (its members are not constrained to being objects).

使用iOS 6.0+ SDK 中的NSHashTable。哈希表以 NSSet 为模型,但有以下区别: 它可以保存对其成员的弱引用。它的成员可以在输入时复制,也可以使用指针标识进行相等和散列。它可以包含任意指针(其成员不限于对象)。

 NSHashTable *hashTable = [NSHashTable 
 hashTableWithOptions:NSPointerFunctionsCopyIn];
 [hashTable addObject:@"foo"];
 [hashTable addObject:@"bar"];
 [hashTable addObject:@100];
 [hashTable removeObject:@"bar"];
 NSLog(@"Members: %@", [hashTable allObjects]);

Use NSMapTablefrom iOS 6.0+ SDK. The map table is modeled after NSDictionary with the following differences: Keys and/or values are optionally held “weakly” such that entries are removed when one of the objects is reclaimed. Its keys or values may be copied on input or may use pointer identity for equality and hashing. It can contain arbitrary pointers (its contents are not constrained to being objects).

使用iOS 6.0+ SDK 中的NSMapTable。映射表是在 NSDictionary 之后建模的,但有以下区别: 键和/或值可选地“弱”保持,以便在回收对象之一时删除条目。它的键或值可以在输入时复制,也可以使用指针标识进行相等和散列。它可以包含任意指针(其内容不限于对象)。

 id delegate = ...;
 NSMapTable *mapTable = [NSMapTable 
 mapTableWithKeyOptions:NSMapTableStrongMemory
                                         valueOptions:NSMapTableWeakMemory];
 [mapTable setObject:delegate forKey:@"foo"];
 NSLog(@"Keys: %@", [[mapTable keyEnumerator] allObjects]);