NSHashTable and NSMapTable
Today, when implementing the play gif function, we can see that there are two unfamiliar classes. Here we can record what we can see:
NSSet and NSDictionary are two common classes, but they assume the memory behavior of the objects by default. For NSSet, the object is strongly referenced, and the value in NSDictionary is the same. The key in NSDictionary is copy. Therefore, when the developer wants to make the objects of NSSet or the values of NSDictionary weak, or the NSDictionary uses an object that does not implement the Protocol as the key, it is troublesome (you need to use the NSValue method valueWithNonretainedObject ).
Starting from iOS6 and mac OS X 10.5, two classes NSHashTable and NSMapTable are provided, which are more common than NSSet and NSDictionary.
NSHashTable is a more common similarity than NSSet. Compared with NSSet/NSMutableSet, NSHashTable has the following features:
- NSSet/NSMutableSet indicates a strong reference to the object. The isEqual method is used to check whether the object is equal and the hash value is obtained using the hash method.
- NSHashTable is variable, and none of them are unchanged.
- NSHashTable can reference weak objects.
- NSHashTable can copy objects during input (addition.
- NSHashTable can contain arbitrary pointers and use pointers for equality or hashing checks.
For example:
•NSHashTable *hashTable = [NSHashTable hashTableWithOptions:NSPointerFunctionsCopyIn];•[hashTable addObject:@"foo"];•[hashTable addObject:@"bar"];•[hashTable addObject:@"foo"];•[hashTable addObject:@42];•NSLog(@"Members: %@", [hashTable allObjects]);
- NSHashTable uses an option for initialization. The following are available options:
- NSHashTableStrongMemory: Same as NSPointerFunctionsStrongMemory. Use this option as the default action, which is the same as the memory policy of NSSet.
- NSHashTableWeakMemory: Same as NSPointerFunctionsWeakMemory. This option uses weak to store objects. when an object is destroyed, it is automatically removed from the set.
- NSHashTableCopyIn: Same as NSPointerFunctionsCopyIn. This option copies objects before they are added to the set.
- NSHashTableObjectPointerPersonality: The same as NSPointerFunctionsObjectPointerPersonality. This option uses pointers for isEqual: And hash.
- NSMapTable corresponds to NSDictionary. Compared with NSDictionary/NSMutableDictionary, NSMapTable has the following features:
- NSDictionary/NSMutableDictionary will copy the corresponding key and strongly reference the corresponding value.
- NSMapTable is variable, and no constant class corresponds to it.
- NSMapTable can have weak references to its key and value. In this case, when the key or value is released, this entry is automatically removed from NSMapTable.
- When adding a (key, value) to NSMapTable, you can set its value to copy.
- NSMapTable can contain any pointer and use the pointer for equality or hashing checks.
- In the following NSMapTable example, the key is not a copy (strongly referenced) and the value is a weak reference.
id delegate = ...;NSMapTable *mapTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];[mapTable setObject:delegate forKey:@"foo"];NSLog(@"Keys: %@", [[mapTable keyEnumerator] allObjects]);
The NSMapTable object uses options to specify the behaviors of keys and values during initialization.
NSMapTableStrongMemory: Specify the corresponding key or value as a strong reference. NSMapTableWeakMemory: Specify the corresponding key or value as a weak reference. NSMapTableCopyIn: Specify the corresponding key or value as copy when it is added to the set. NSMapTableObjectPointerPersonality: This option directly uses the pointer for isEqual: And hash.
If you see this article and feel helpful to you, you can follow my website: my website.