objc

Instance Methods | Class Methods

The NXMap class. More...

#import <Foundation/Foundation.h>

Inheritance diagram for NXMap:
Inheritance graph
[legend]
Collaboration diagram for NXMap:
Collaboration graph
[legend]

Instance Methods

(unsigned int) - count
 Returns the number of elements in the map. More...
 
(size_t) - capacity
 Returns the capacity of the map. More...
 
(NXArray *) - allKeys
 Returns an array containing all keys stored in the map. More...
 
(NXArray *) - allObjects
 Returns an array containing all objects stored in the map. More...
 
(BOOL- setObject:forKey:
 Stores an object in the map with the specified key. More...
 
(id- objectForKey:
 Retrieves an object from the map by its key. More...
 
(BOOL- removeObjectForKey:
 Removes an object from the map by its key. More...
 
(void) - removeAllObjects
 Removes all objects from the map. More...
 
- Instance Methods inherited from NXObject
(id- retain
 Increases the retain count of the receiver. More...
 
(void) - release
 Decreases the retain count of the receiver. More...
 
(id- autorelease
 Adds the receiver to the autorelease pool.
 
- Instance Methods inherited from Object
(void) - dealloc
 Free resources for an existing instance.
 
(id- init
 Initialize the instance, after allocation. More...
 
(Class- class
 Returns the class of the instance. More...
 
(Class- superclass
 Returns the superclass of the instance. More...
 
(BOOL- isEqual:
 Compares the receiver to another object for equality. More...
 
(BOOL- isKindOfClass:
 Returns a Boolean value that indicates whether the receiver is an instance of a given class. More...
 
(BOOL- conformsTo:
 Checks if the receiver's class conforms to a protocol. More...
 
(NXString *) - description
 Returns a string that represents the instance. More...
 
- Instance Methods inherited from <ObjectProtocol>
(BOOL- respondsToSelector:
 Checks if the receiver responds to a selector. More...
 
- Instance Methods inherited from <JSONProtocol>
(NXString *) - JSONString
 Returns a JSON representation of the instance. More...
 
(size_t) - JSONBytes
 Returns the appropriate capacity for the JSON representation of the instance. More...
 
- Instance Methods inherited from <CollectionProtocol>
(BOOL- containsObject:
 Returns YES if the collection contains the specified object. More...
 

Class Methods

(NXMap *) + new
 Returns a new empty NXMap instance.
 
(NXMap *) + mapWithCapacity:
 Returns a new NXMap instance with the specified initial capacity. More...
 
(NXMap *) + mapWithObjectsAndKeys:
 Creates and returns a new NXMap initialized with alternating objects and keys. More...
 
(NXMap *) + mapWithObjects:forKeys:
 Creates and returns a new NXMap initialized with objects from one array and corresponding keys from another array. More...
 
- Class Methods inherited from NXObject
(id+ allocWithZone:
 Allocates a new instance of an object in a specific memory zone. More...
 
- Class Methods inherited from Object
(void) + initialize
 Performs one-time initialization for the class. More...
 
(id+ alloc
 Allocate a new class instance. More...
 
(Class+ class
 Returns the class object. More...
 
(Class+ superclass
 Returns the superclass of the class. More...
 
(const char *) + name
 Returns the name of the class. More...
 
(BOOL+ conformsTo:
 Checks if the class conforms to a protocol. More...
 
(NXString *) + description
 Returns a string that represents the class. More...
 

Additional Inherited Members

- Protected Attributes inherited from NXObject
id _zone
 The memory zone where the object is allocated.
 
unsigned short _retain
 The retain count of the object.
 
id _next
 The next object in an autorelease pool.
 
- Protected Attributes inherited from Object
Class isa
 A pointer to the object's class structure. More...
 

Detailed Description

The NXMap class.

NXMap represents a map that can store key-value pairs, where keys are strings and values are arbitrary objects.

The map uses an optimized hash table implementation for efficient storage and retrieval of key-value pairs. Keys must be string objects that implement the NXConstantStringProtocol.

Objects stored in the map are retained and will be released when the map is deallocated, when an object is removed, or when removeAllObjects is called.

Definition at line 29 of file NXMap.h.

Method Documentation

◆ allKeys()

- (NXArray *) allKeys

Returns an array containing all keys stored in the map.

Returns
An NXArray containing all keys in the map, or an empty array if the map is empty.

The order of keys in the returned array is not guaranteed to match the order of insertion. The array contains only the keys stored in the map, not the associated objects.

◆ allObjects()

- (NXArray *) allObjects

Returns an array containing all objects stored in the map.

Returns
An NXArray containing all objects in the map, or an empty array if the map is empty.

The order of objects in the returned array is not guaranteed to match the order of insertion. The array contains only the values stored in the map, not the keys.

◆ capacity()

- (size_t) capacity

Returns the capacity of the map.

Returns
The maximum number of elements the map can hold before resizing.

◆ count()

- (unsigned int) count

Returns the number of elements in the map.

Returns
The number of elements in the map.

Reimplemented from <CollectionProtocol>.

◆ mapWithCapacity:()

+ (NXMap *) mapWithCapacity: (size_t)  capacity

Returns a new NXMap instance with the specified initial capacity.

Parameters
capacityThe initial capacity of the map.
Returns
A new NXMap instance with the specified capacity.

◆ mapWithObjects:forKeys:()

+ (NXMap *) mapWithObjects: (NXArray *)  objects
forKeys: (NXArray *)  keys 

Creates and returns a new NXMap initialized with objects from one array and corresponding keys from another array.

Parameters
objectsAn NXArray containing the objects to store in the map. Must not be nil and must contain the same number of elements as keys.
keysAn NXArray containing the keys to associate with the objects. Must not be nil, must contain the same number of elements as objects, and all elements must implement the NXConstantStringProtocol.
Returns
A new autoreleased NXMap instance containing the key-value pairs formed by pairing elements from the two arrays, or nil if an error occurs.

This method creates a new map by pairing elements at corresponding indices from the two input arrays. The first element of the objects array is paired with the first element of the keys array, the second with the second, and so on.

Example usage:

NXArray *objects = [NXArray arrayWithObjects:@"value1", @"value2", nil];
NXArray *keys = [NXArray arrayWithObjects:@"key1", @"key2", nil];
NXMap *map = [NXMap mapWithObjects:objects forKeys:keys];

Both arrays must contain the same number of elements. If they differ in size, the behavior is undefined.

◆ mapWithObjectsAndKeys:()

+ (NXMap *) mapWithObjectsAndKeys: (id firstObject
,   OBJC_REQUIRES_NIL_TERMINATION 

Creates and returns a new NXMap initialized with alternating objects and keys.

Parameters
firstObjectThe first object to store in the map, followed by its key, then the next object, its key, and so on. The list must be terminated with nil.
...A nil-terminated list of alternating objects and keys.
Returns
A new autoreleased NXMap instance containing the specified key-value pairs, or nil if an error occurs during creation.

This method creates a new map by taking pairs of arguments where each odd argument is an object and each even argument is its corresponding key. The argument list must be terminated with nil. Keys must implement the NXConstantStringProtocol.

Example usage:

NXMap *map = [NXMap mapWithObjectsAndKeys:
@"value1", @"key1",
@"value2", @"key2",
nil];

◆ objectForKey:()

- (id) objectForKey: (id< NXConstantStringProtocol, RetainProtocol >)  key

Retrieves an object from the map by its key.

Parameters
keyThe string key to look up. Must be non-nil and implement both the NXConstantStringProtocol and RetainProtocol.
Returns
The object associated with the given key, or nil if the key is not found in the map.

◆ removeAllObjects()

- (void) removeAllObjects

Removes all objects from the map.

This method effectively clears all key-value pairs from the map. All previously stored objects are released, and any references to them become invalid. The map remains fully functional after this operation and can accept new key-value pairs immediately.

◆ removeObjectForKey:()

- (BOOL) removeObjectForKey: (id< NXConstantStringProtocol, RetainProtocol >)  key

Removes an object from the map by its key.

Parameters
keyThe string key of the object to remove. Must be non-nil and implement both the NXConstantStringProtocol and RetainProtocol.
Returns
YES if an object was found and successfully removed, NO if the key was not found in the map or if the operation failed.

When an object is successfully removed, it is released by the map. The map's count is decremented by one. If the key is not found, the map remains unchanged.

◆ setObject:forKey:()

- (BOOL) setObject: (id anObject
forKey: (id< NXConstantStringProtocol, RetainProtocol >)  key 

Stores an object in the map with the specified key.

Parameters
anObjectThe object to store in the map. Must be non-nil.
keyThe string key to associate with the object. Must be non-nil and implement both the NXConstantStringProtocol and RetainProtocol.
Returns
YES if the object was successfully stored, NO if the operation failed (e.g., due to memory allocation failure).

If a key already exists in the map, the new object will overwrite the existing one. The object is retained by the map.


The documentation for this class was generated from the following file: