Up

NSDictionary class reference

Authors

Andrew Kachites McCallum (mccallum@gnu.ai.mit.edu)
Adam Fedor (fedor@boulder.colorado.edu)

Date: Generated at 2023-12-20 19:35:39 -0500

Copyright: (C) 1995, 1996, 1997 Free Software Foundation, Inc.


Contents -

  1. Software documentation for the NSDictionary class
  2. Software documentation for the NSMutableDictionary class

Software documentation for the NSDictionary class

NSDictionary : NSObject

Declared in:
Foundation/NSDictionary.h
Conforms to:
NSCoding
NSCopying
NSMutableCopying
NSFastEnumeration
Availability: OpenStep

This class and its subclasses store key-value pairs, where the key and the value are objects. A great many utility methods for working with dictionaries are provided as part of this class, including the ability to retrieve multiple entries simultaneously, obtain sorted contents, and read/write from/to a serialized representation.

The keys are copied and values are retained by the implementation, and both are released when either their entry is dropped or the entire dictionary is deallocated.
As in the OS X implementation, keys must therefore implement the <NSCopying> protocol.

Objects of this class are immutable. For a mutable version, use the NSMutableDictionary subclass.

The basic functionality in NSDictionary is similar to that in Java's HashMap, and like that class it includes no locking code and is not thread-safe. If the contents will be modified and accessed from multiple threads you should enclose critical operations within locks (see NSLock ).

Method summary

dictionary 

+ (instancetype) dictionary;
Availability: OpenStep

Returns a new autoreleased empty dictionary.

dictionaryWithContentsOfFile: 

+ (instancetype) dictionaryWithContentsOfFile: (NSString*)path;
Availability: OpenStep

Returns a dictionary using the file located at path. The file must be a property list containing a dictionary as its root object.

dictionaryWithContentsOfURL: 

+ (instancetype) dictionaryWithContentsOfURL: (NSURL*)aURL;
Availability: MacOS-X 10.0.0

Returns a dictionary using the contents of aURL. The URL must be a property list containing a dictionary as its root object.

dictionaryWithDictionary: 

+ (instancetype) dictionaryWithDictionary: (NSDictionary*)otherDictionary;
Availability: OpenStep

Returns a newly created dictionary with the keys and objects of otherDictionary. (The keys and objects are not copied.)

dictionaryWithObject: forKey: 

+ (instancetype) dictionaryWithObject: (GS_GENERIC_TYPE(ValT))object forKey: (GS_GENERIC_TYPE(KeyT))key;
Availability: OpenStep

Returns a dictionary containing only one object which is associated with a key.

dictionaryWithObjects: forKeys: 

+ (instancetype) dictionaryWithObjects: (GS_GENERIC_CLASS(NSArray,ValT)*)objects forKeys: (GS_GENERIC_CLASS(NSArray,KeyT)*)keys;
Availability: OpenStep

Returns a dictionary created using the given objects and keys. The two arrays must have the same length. The n th element of the objects array is associated with the n th element of the keys array.

dictionaryWithObjects: forKeys: count: 

+ (instancetype) dictionaryWithObjects: (const GS_GENERIC_TYPE(ValT)[])objects forKeys: (const GS_GENERIC_TYPE_F(KeyT,id<NSCopying>)[])keys count: (NSUInteger)count;
Availability: OpenStep

Returns a dictionary created using the given objects and keys. The two arrays must have the same size. The n th element of the objects array is associated with the n th element of the keys array.

dictionaryWithObjectsAndKeys: ,...

+ (instancetype) dictionaryWithObjectsAndKeys: (id)firstObject,...;
Availability: OpenStep

Returns a dictionary created using the list given as argument. The list is alternately composed of objects and keys and terminated by nil. Thus, the list's length must be even, followed by nil.

allKeys 

- (GS_GENERIC_CLASS(NSArray,KeyT)*) allKeys;
Availability: OpenStep

Returns an array containing all the dictionary's keys.

allKeysForObject: 

- (GS_GENERIC_CLASS(NSArray,KeyT)*) allKeysForObject: (GS_GENERIC_TYPE(ValT))anObject;
Availability: OpenStep

Returns an array containing all the dictionary's keys that are associated with anObject.

allValues 

- (GS_GENERIC_CLASS(NSArray,ValT)*) allValues;
Availability: OpenStep

Returns an array containing all the dictionary's objects.

count 

- (NSUInteger) count;
Availability: OpenStep

Returns an unsigned integer which is the number of elements stored in the dictionary.

description 

- (NSString*) description;
Availability: OpenStep

Returns the result of invoking -descriptionWithLocale:indent: with a nil locale and zero indent.

descriptionInStringsFileFormat 

- (NSString*) descriptionInStringsFileFormat;
Availability: OpenStep

Returns the receiver as a text property list strings file format.
See [NSString -propertyListFromStringsFileFormat] for details.
The order of the items is undefined.

descriptionWithLocale: 

- (NSString*) descriptionWithLocale: (id)locale;
Availability: OpenStep

Returns the result of invoking -descriptionWithLocale:indent: with a zero indent.

descriptionWithLocale: indent: 

- (NSString*) descriptionWithLocale: (id)locale indent: (NSUInteger)level;
Availability: OpenStep

Returns the receiver as a text property list in the traditional format.
See [NSString -propertyList] for details.
If locale is nil, no formatting is done, otherwise entries are formatted according to the locale, and indented according to level.
Unless locale is nil, a level of zero indents items by four spaces, while a level of one indents them by a tab.
If the keys in the dictionary respond to [NSString -compare:] , the items are listed by key in ascending order. If not, the order in which the items are listed is undefined.

enumerateKeysAndObjectsUsingBlock: 

- (void) enumerateKeysAndObjectsUsingBlock: (GSKeysAndObjectsEnumeratorBlock)aBlock;
Availability: MacOS-X 10.6.0

Description forthcoming.

enumerateKeysAndObjectsWithOptions: usingBlock: 

- (void) enumerateKeysAndObjectsWithOptions: (NSEnumerationOptions)opts usingBlock: (GSKeysAndObjectsEnumeratorBlock)aBlock;
Availability: MacOS-X 10.6.0

Description forthcoming.

getObjects: andKeys: 

- (void) getObjects: (__unsafe_unretained GS_GENERIC_TYPE(ValT)[])objects andKeys: (__unsafe_unretained GS_GENERIC_TYPE_F(KeyT,id<NSCopying>)[])keys;
Availability: OpenStep

Description forthcoming.

init 

- (instancetype) init;
Availability: OpenStep

In MacOS-X class clusters do not have designated initialisers, and there is a general rule that -init is treated as the designated initialiser of the class cluster, but that other intitialisers may not work s expected an would need to be individually overridden in any subclass.

GNUstep tries to make it easier to subclass a class cluster, by making class clusters follow the same convention as normal classes, so the designated initialiser is the richest initialiser. This means that all other initialisers call the documented designated initialiser (which calls -init only for MacOS-X compatibility), and anyone writing a subclass only needs to override that one initialiser in order to have all the other ones work.

For MacOS-X compatibility, you may also need to override various other initialisers. Exactly which ones, you will need to determine by trial on a MacOS-X system... and may vary between releases of MacOS-X. So to be safe, on MacOS-X you probably need to re-implement all the class cluster initialisers you might use in conjunction with your subclass.


initWithContentsOfFile: 

- (instancetype) initWithContentsOfFile: (NSString*)path;
Availability: OpenStep

Initialises the dictionary with the contents of the specified file, which must contain a dictionary in property-list format.

In GNUstep, the property-list format may be either the OpenStep format (ASCII data), or the MacOS-X format (UTF-8 XML data)... this method will recognise which it is.

If there is a failure to load the file for any reason, the receiver will be released and the method will return nil.

Works by invoking [NSString -initWithContentsOfFile:] and [NSString -propertyList] then checking that the result is a dictionary.


initWithContentsOfURL: 

- (instancetype) initWithContentsOfURL: (NSURL*)aURL;
Availability: MacOS-X 10.0.0

Initialises the dictionary with the contents of the specified URL, which must contain a dictionary in property-list format.

In GNUstep, the property-list format may be either the OpenStep format (ASCII data), or the MacOS-X format (UTF-8 XML data)... this method will recognise which it is.

If there is a failure to load the URL for any reason, the receiver will be released and the method will return nil.

Works by invoking [NSString -initWithContentsOfURL:] and [NSString -propertyList] then checking that the result is a dictionary.


initWithDictionary: 

- (instancetype) initWithDictionary: (GS_GENERIC_CLASS(NSDictionary,KeyT,ValT)*)otherDictionary;
Availability: OpenStep

Initializes with the keys and objects of otherDictionary. (The keys and objects are not copied.)

initWithDictionary: copyItems: 

- (id) initWithDictionary: (GS_GENERIC_CLASS(NSDictionary,KeyT,ValT)*)other copyItems: (BOOL)shouldCopy;
Availability: OpenStep

Initialise dictionary with the keys and values of otherDictionary. If the shouldCopy flag is YES then the values are copied into the newly initialised dictionary, otherwise they are simply retained, on the assumption that it is safe to retain the keys from another dictionary since that other dictionary mwill have copied the keys originally to ensure that they are immutable.

initWithObjects: forKeys: 

- (id) initWithObjects: (GS_GENERIC_CLASS(NSArray,KeyT)*)objects forKeys: (GS_GENERIC_CLASS(NSArray,ValT)*)keys;
Availability: OpenStep

Initialises a dictionary created using the given objects and keys. The two arrays must have the same size. The n th element of the objects array is associated with the n th element of the keys array.

initWithObjects: forKeys: count: 

- (id) initWithObjects: (const GS_GENERIC_TYPE(ValT)[])objects forKeys: (const GS_GENERIC_TYPE_F(KeyT,id<NSCopying>)[])keys count: (NSUInteger)count;
Availability: OpenStep

This is a designated initialiser for the class.
Subclasses must override this method.
Initializes contents to the given objects and keys. The two arrays must have the same size. The n th element of the objects array is associated with the n th element of the keys array.
Calls -init (which does nothing but maintain MacOS-X compatibility), and needs to be re-implemented in subclasses in order to have all other initialisers work.

initWithObjectsAndKeys: ,...

- (id) initWithObjectsAndKeys: (GS_GENERIC_TYPE(ValT))firstObject,...;
Availability: OpenStep

Initialises a dictionary created using the list given as argument. The list is alternately composed of objects and keys and terminated by nil. Thus, the list's length must be even, followed by nil.

isEqualToDictionary: 

- (BOOL) isEqualToDictionary: (GS_GENERIC_CLASS(NSDictionary,KeyT,ValT)*)other;
Availability: OpenStep

Two dictionaries are equal if they each hold the same number of entries, each key in one isEqual to a key in the other, and, for a given key, the corresponding value objects also satisfy isEqual.

keyEnumerator 

- (GS_GENERIC_CLASS(NSEnumerator,KeyT)*) keyEnumerator;
Availability: OpenStep

Return an enumerator object containing all the keys of the dictionary.

keysOfEntriesPassingTest: 

- (GS_GENERIC_CLASS(NSSet,KeyT)*) keysOfEntriesPassingTest: (GSKeysAndObjectsPredicateBlock)aPredicate;
Availability: MacOS-X 10.6.0

Description forthcoming.

keysOfEntriesWithOptions: passingTest: 

- (GS_GENERIC_CLASS(NSSet,KeyT)*) keysOfEntriesWithOptions: (NSEnumerationOptions)opts passingTest: (GSKeysAndObjectsPredicateBlock)aPredicate;
Availability: MacOS-X 10.6.0

Description forthcoming.

keysSortedByValueUsingComparator: 

- (GS_GENERIC_CLASS(NSArray,ValT)*) keysSortedByValueUsingComparator: (NSComparator)cmptr;
Availability: OpenStep

Description forthcoming.

keysSortedByValueUsingSelector: 

- (GS_GENERIC_CLASS(NSArray,ValT)*) keysSortedByValueUsingSelector: (SEL)comp;
Availability: OpenStep

Returns ordered array of the keys sorted according to the values they correspond to. To sort the values, a message with selector comp is send to each value with another value as argument, as in [a comp: b]. The comp method should return NSOrderedSame, NSOrderedAscending, or NSOrderedDescending as appropriate.

keysSortedByValueWithOptions: usingComparator: 

- (GS_GENERIC_CLASS(NSArray,ValT)*) keysSortedByValueWithOptions: (NSSortOptions)opts usingComparator: (NSComparator)cmptr;
Availability: OpenStep

Description forthcoming.

objectEnumerator 

- (GS_GENERIC_CLASS(NSEnumerator,ValT)*) objectEnumerator;
Availability: OpenStep

Return an enumerator object containing all the objects of the dictionary.

objectForKey: 

- (GS_GENERIC_TYPE(ValT)) objectForKey: (GS_GENERIC_TYPE(KeyT))aKey;
Availability: OpenStep

Returns the object in the dictionary corresponding to aKey, or nil if the key is not present.

objectForKeyedSubscript: 

- (GS_GENERIC_TYPE(ValT)) objectForKeyedSubscript: (GS_GENERIC_TYPE(KeyT))aKey;
Availability: OpenStep

Method called by array subscripting.

objectsForKeys: notFoundMarker: 

- (GS_GENERIC_CLASS(NSArray,ValT)*) objectsForKeys: (GS_GENERIC_CLASS(NSArray,KeyT)*)keys notFoundMarker: (GS_GENERIC_TYPE(ValT))marker;
Availability: OpenStep

Multiple version of -objectForKey: . Objects for each key in keys are looked up and placed into return array in same order. For each key that has no corresponding value in this dictionary, marker is put into the array in its place.

valueForKey: 

- (GS_GENERIC_TYPE(ValT)) valueForKey: (NSString*)key;
Availability: MacOS-X 10.0.0

Default implementation for this class is to return the value stored in the dictionary under the specified key, or nil if there is no value.
However, if the key begins with '@' that character is stripped from it and the superclass implementation of the method is used.

writeToFile: atomically: 

- (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile;
Availability: OpenStep

Writes the contents of the dictionary to the file specified by path. The file contents will be in property-list format... under GNUstep this is either OpenStep style (ASCII characters using \U hexadecimal escape sequences for unicode), or MacOS-X style (XML in the UTF8 character set).

If the useAuxiliaryFile flag is YES, the file write operation is atomic... the data is written to a temporary file, which is then renamed to the actual file name.

If the conversion of data into the correct property-list format fails or the write operation fails, the method returns NO, otherwise it returns YES.

NB. The fact that the file is in property-list format does not necessarily mean that it can be used to reconstruct the dictionary using the -initWithContentsOfFile: method. If the original dictionary contains non-property-list objects, the descriptions of those objects will have been written, and reading in the file as a property-list will result in a new dictionary containing the string descriptions.


writeToURL: atomically: 

- (BOOL) writeToURL: (NSURL*)url atomically: (BOOL)useAuxiliaryFile;
Availability: MacOS-X 10.0.0

Writes the contents of the dictionary to the specified url. This functions just like -writeToFile:atomically: except that the output may be written to any URL, not just a local file.


Software documentation for the NSMutableDictionary class

NSMutableDictionary : NSDictionary

Declared in:
Foundation/NSDictionary.h
Availability: OpenStep

Mutable version of NSDictionary .
Method summary

dictionaryWithCapacity: 

+ (instancetype) dictionaryWithCapacity: (NSUInteger)numItems;
Availability: OpenStep

Returns an empty dictionary with memory preallocated for given number of entries. Although memory space will be grown as needed when entries are added, this can avoid the reallocate-and-copy process if the size of the ultimate contents is known in advance.

addEntriesFromDictionary: 

- (void) addEntriesFromDictionary: (GS_GENERIC_CLASS(NSDictionary,KeyT,ValT)*)otherDictionary;
Availability: OpenStep

Merges information from otherDictionary into the receiver. If a key exists in both dictionaries, the value from otherDictionary replaces that which was originally in the receiver.

initWithCapacity: 

- (instancetype) initWithCapacity: (NSUInteger)numItems;
Availability: OpenStep

This is a designated initialiser for the class.
Subclasses must override this method.
Initializes an empty dictionary with memory preallocated for given number of entries. Although memory space will be grown as needed when entries are added, this can avoid the reallocate-and-copy process if the size of the ultimate contents is known in advance.
Calls -init (which does nothing but maintain MacOS-X compatibility), and needs to be re-implemented in subclasses in order to have all other initialisers work.

removeAllObjects 

- (void) removeAllObjects;
Availability: OpenStep

Clears out this dictionary by removing all entries.

removeObjectForKey: 

- (void) removeObjectForKey: (GS_GENERIC_TYPE(KeyT))aKey;
Availability: OpenStep

Removes the object with the specified key from the receiver. This method is primitive.
Remove key-value mapping for given key aKey. No error if there is no mapping for the key. A warning will be generated if aKey is nil.

removeObjectsForKeys: 

- (void) removeObjectsForKeys: (GS_GENERIC_CLASS(NSArray,KeyT)*)keyArray;
Availability: OpenStep

Remove entries specified by the given keyArray. No error is generated if no mapping exists for a key or one is nil , although a console warning is produced in the latter case.

setDictionary: 

- (void) setDictionary: (GS_GENERIC_CLASS(NSDictionary,KeyT,ValT)*)otherDictionary;
Availability: OpenStep

Remove all entries, then add all entries from otherDictionary.

setObject: forKey: 

- (void) setObject: (GS_GENERIC_TYPE(ValT))anObject forKey: (GS_GENERIC_TYPE(KeyT))aKey;
Availability: OpenStep

Adds entry for aKey, mapping to anObject. If either is nil, an exception is raised. If aKey already in dictionary, the value it maps to is silently replaced. The value anObject is retained, but aKey is copied (because a dictionary key must be immutable) and must therefore implement the <NSCopying> protocol.)

setObject: forKeyedSubscript: 

- (void) setObject: (GS_GENERIC_TYPE(ValT))anObject forKeyedSubscript: (GS_GENERIC_TYPE(KeyT))aKey;
Availability: OpenStep

Method called by array subscripting.

setValue: forKey: 

- (void) setValue: (GS_GENERIC_TYPE(ValT))value forKey: (NSString*)key;
Availability: MacOS-X 10.0.0

Default implementation for this class is equivalent to the -setObject:forKey: method unless value is nil, in which case it is equivalent to -removeObjectForKey:

takeStoredValue: forKey: 

- (void) takeStoredValue: (GS_GENERIC_TYPE(ValT))value forKey: (NSString*)key;
Availability: MacOS-X 10.0.0

Default implementation for this class is equivalent to the -setObject:forKey: method unless value is nil, in which case it is equivalent to -removeObjectForKey:

takeValue: forKey: 

- (void) takeValue: (GS_GENERIC_TYPE(ValT))value forKey: (NSString*)key;
Availability: MacOS-X 10.0.0

Default implementation for this class is equivalent to the -setObject:forKey: method unless value is nil, in which case it is equivalent to -removeObjectForKey:


Up