Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.5 and later. |
Declared in | NSMapTable.h |
Companion guides |
NSMapTable
is a mutable collection modeled after NSDictionary
but provides different options, in particular to support weak relationships in a garbage-collected environment.
NSMapTable
is modeled after NSDictionary
but offers different behaviors:
It can hold weak references to its keys and/or values.
Keys and/or values held "weakly" in a manner that entries are removed when one of the objects is collected under garbage collection.
If you are not using garbage collection, you must explicitly remove entries as you would from a dictionary. In addition to being held weakly, 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).
You can configure an NSMapTable
instance to operate on arbitrary pointers and not just objects, although typically you are encouraged to use the C function API for void *
pointers. The object-based API (such as setObject:forKey:
) will not work for non-object pointers without type-casting.
To configure an NSMapTable
instance for pointer use, you can: create or initialize it using mapTableWithKeyOptions:valueOptions:
or initWithKeyOptions:valueOptions:capacity:
and the appropriate NSPointerFunctionsOptions
options; or initialize it with initWithKeyPointerFunctions:valuePointerFunctions:capacity:
and appropriate instances of NSPointerFunctions
. Note that only the options listed in “Personality Options”
guarantee that the rest of the API will work correctly—including copying, archiving, and fast enumeration. If you use other NSPointerFunctions
options, the map table may not work correctly, or may not even be initialized correctly.
– initWithKeyOptions:valueOptions:capacity:
+ mapTableWithKeyOptions:valueOptions:
– initWithKeyPointerFunctions:valuePointerFunctions:capacity:
+ mapTableWithStrongToStrongObjects
+ mapTableWithWeakToStrongObjects
+ mapTableWithStrongToWeakObjects
+ mapTableWithWeakToWeakObjects
Returns a new map table, initialized with the given options
+ (id)mapTableWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions
A bit field that specifies the options for the keys in the map table.
Important: Not all values of NSPointerFunctionsOptions
are valid for NSMapTable
. For values that are guaranteed to work correctly, see “Personality Options”
.
A bit field that specifies the options for the values in the map table.
Important: Not all values of NSPointerFunctionsOptions
are valid for NSMapTable
. For values that are guaranteed to work correctly, see “Personality Options”
.
A new map table, initialized with the given options.
– initWithKeyOptions:valueOptions:capacity:
– initWithKeyPointerFunctions:valuePointerFunctions:capacity:
NSMapTable.h
Returns a new map table object which has strong references to the keys and values.
+ (id)mapTableWithStrongToStrongObjects
A new map table object which has strong references to the keys and values.
NSMapTable.h
Returns a new map table object which has strong references to the keys and weak references to the values.
+ (id)mapTableWithStrongToWeakObjects
A new map table object which has strong references to the keys and weak references to the values.
NSMapTable.h
Returns a new map table object which has weak references to the keys and strong references to the values.
+ (id)mapTableWithWeakToStrongObjects
A new map table object which has weak references to the keys and strong references to the values.
NSMapTable.h
Returns a new map table object which has weak references to the keys and values.
+ (id)mapTableWithWeakToWeakObjects
A new map table object which has weak references to the keys and values.
NSMapTable.h
Returns the number of key-value pairs in the receiver.
- (NSUInteger)count
The number of key-value pairs in the receiver.
NSMapTable.h
Returns a dictionary representation of the receiver.
- (NSDictionary *)dictionaryRepresentation
A dictionary representation of the receiver.
The receiver’s contents must be objects.
NSMapTable.h
Returns a map table, initialized with the given options.
- (id)initWithKeyOptions:(NSPointerFunctionsOptions)keyOptions valueOptions:(NSPointerFunctionsOptions)valueOptions capacity:(NSUInteger)initialCapacity
A bit field that specifies the options for the keys in the map table.
Important: Not all values of NSPointerFunctionsOptions
are valid for NSMapTable
. For values that are guaranteed to work correctly, see “Personality Options”
.
A bit field that specifies the options for the values in the map table.
Important: Not all values of NSPointerFunctionsOptions
are valid for NSMapTable
. For values that are guaranteed to work correctly, see “Personality Options”
.
The initial capacity of the receiver. This is just a hint; the map table may subsequently grow and shrink as required.
A map table initialized using the given options.
values must contain entries at all the indexes specified in keys.
+ mapTableWithKeyOptions:valueOptions:
– initWithKeyPointerFunctions:valuePointerFunctions:capacity:
NSMapTable.h
Returns a map table, initialized with the given functions.
- (id)initWithKeyPointerFunctions:(NSPointerFunctions *)keyFunctions valuePointerFunctions:(NSPointerFunctions *)valueFunctions capacity:(NSUInteger)initialCapacity
The functions the receiver uses to manage keys.
Important: Not all values of NSPointerFunctionsOptions
are valid for NSMapTable
. For values that are guaranteed to work correctly, see “Personality Options”
.
The functions the receiver uses to manage values.
Important: Not all values of NSPointerFunctionsOptions
are valid for NSMapTable
. For values that are guaranteed to work correctly, see “Personality Options”
.
The initial capacity of the receiver. This is just a hint; the map table may subsequently grow and shrink as required.
A map table, initialized with the given functions.
NSMapTable.h
Returns an enumerator object that lets you access each key in the receiver.
- (NSEnumerator *)keyEnumerator
An enumerator object that lets you access each key in the receiver.
The following code fragment illustrates how you might use the method.
NSEnumerator *enumerator = [myMapTable keyEnumerator]; |
id value; |
while ((value = [enumerator nextObject])) { |
/* code that acts on the map table's keys */ |
} |
See also NSFastEnumeration
.
NSMapTable.h
Returns the pointer functions the receiver uses to manage keys.
- (NSPointerFunctions *)keyPointerFunctions
The pointer functions the receiver uses to manage keys.
NSMapTable.h
Returns an enumerator object that lets you access each value in the receiver.
- (NSEnumerator *)objectEnumerator
An enumerator object that lets you access each value in the receiver.
The following code fragment illustrates how you might use the method.
NSEnumerator *enumerator = [myMapTable objectEnumerator]; |
id value; |
while ((value = [enumerator nextObject])) { |
/* code that acts on the map table's values */ |
} |
See also NSFastEnumeration
.
NSMapTable.h
Returns a the value associated with a given key.
- (id)objectForKey:(id)aKey
The key for which to return the corresponding value.
The value associated with aKey, or nil
if no value is associated with aKey.
NSMapTable.h
Empties the receiver of its entries.
- (void)removeAllObjects
NSMapTable.h
Removes a given key and its associated value from the receiver.
- (void)removeObjectForKey:(id)aKey
The key to remove.
Does nothing if aKey does not exist.
NSMapTable.h
Adds a given key-value pair to the receiver.
- (void)setObject:(id)anObject forKey:(id)aKey
The value for aKey. This value must not be nil
.
The key for anObject. This value must not be nil
.
NSMapTable.h
Returns the pointer functions the receiver uses to manage values.
- (NSPointerFunctions *)valuePointerFunctions
The pointer functions the receiver uses to manage values.
NSMapTable.h
Constants used as components in a bitfield to specify the behavior of elements (keys and values) in an NSMapTable
object.
enum { NSMapTableStrongMemory = 0, NSMapTableZeroingWeakMemory = NSPointerFunctionsZeroingWeakMemory, NSMapTableCopyIn = NSPointerFunctionsCopyIn, NSMapTableObjectPointerPersonality = NSPointerFunctionsObjectPointerPersonality };
NSMapTableStrongMemory
Specifies a strong reference from the map table to its contents.
Equal to NSPointerFunctionsStrongMemory
.
Available in Mac OS X v10.5 and later.
Declared in NSMapTable.h
.
NSMapTableZeroingWeakMemory
Specifies a zeroing weak reference from the map table to its contents.
Equal to NSPointerFunctionsZeroingWeakMemory
.
Available in Mac OS X v10.5 and later.
Declared in NSMapTable.h
.
NSMapTableCopyIn
Use the memory acquire function to allocate and copy items on input (see acquireFunction
[NSPointerFunctions
]).
Equal to NSPointerFunctionsCopyIn
.
Available in Mac OS X v10.5 and later.
Declared in NSMapTable.h
.
NSMapTableObjectPointerPersonality
Use shifted pointer hash and direct equality, object description.
Equal to NSPointerFunctionsObjectPointerPersonality
.
Available in Mac OS X v10.5 and later.
Declared in NSMapTable.h
.
NSMapTable.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-07-22)