Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Declared in | NSDictionary.h NSFileManager.h NSKeyValueCoding.h |
Companion guides | |
Related sample code |
The NSDictionary
class declares the programmatic interface to objects that manage immutable associations of keys and values. Use this class or its subclass NSMutableDictionary
when you need a convenient and efficient way to retrieve data associated with an arbitrary key. (For convenience, we use the term dictionary to refer to any instance of one of these classes without specifying its exact class membership.)
A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key and a second object that is that key’s value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:
). In general, a key can be any object (provided that it conforms to the NSCopying
protocol—see below), but note that when using key-value coding the key must be a string (see Key-Value Coding Fundamentals). Neither a key nor a value can be nil
; if you need to represent a null value in a dictionary, you should use NSNull
.
An instance of NSDictionary
is an immutable dictionary: you establish its entries when it’s created and cannot modify them afterward. An instance of NSMutableDictionary
is a mutable dictionary: you can add or delete entries at any time, and the object automatically allocates memory as needed. The dictionary classes adopt the NSCopying
and NSMutableCopying
protocols, making it convenient to convert a dictionary of one type to the other.
NSDictionary
and NSMutableDictionary
are part of a class cluster, so the objects you create with this interface are not actual instances of the these two classes. Rather, the instances belong to one of their private subclasses. Although a dictionary’s class is private, its interface is public, as declared by these abstract superclasses, NSDictionary
and NSMutableDictionary
.
Internally, a dictionary uses a hash table to organize its storage and to provide rapid access to a value given the corresponding key. However, the methods defined in this cluster insulate you from the complexities of working with hash tables, hashing functions, or the hashed value of keys. The methods described below take keys directly, not their hashed form.
Methods that add entries to dictionaries—whether as part of initialization (for all dictionaries) or during modification (for mutable dictionaries)—copy each key argument (keys must conform to the NSCopying
protocol) and add the copies to the dictionary. Each corresponding value object receives a retain
message to ensure that it won’t be deallocated before the dictionary is through with it.
You can enumerate the contents of a dictionary by key or by value using the NSEnumerator
object returned by keyEnumerator and objectEnumerator
respectively. On Mac OS X v10.5 and later, NSDictionary
supports the NSFastEnumeration
protocol. You can use the for…in
construct to enumerate the keys of a dictionary, as illustrated in the following example.
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil]; |
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil]; |
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; |
for (id key in dictionary) { |
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); |
} |
Three primitive methods of NSDictionary
—count
, objectForKey:
, and keyEnumerator
—provide the basis for all of the other methods in its interface. The count method returns the number of entries in the dictionary. objectForKey: returns the value associated with a given key. keyEnumerator returns an object that lets you iterate through each of the keys in the dictionary. The other methods declared here operate by invoking one or more of these primitives. The non-primitive methods provide convenient ways of accessing multiple entries at once.
You can use the description...
and writeToFile:atomically:
methods to write a property list representation of a dictionary to a string or to a file, respectively. These are not intended to be used for general persistent storage of your custom data objects—see instead Archives and Serializations Programming Guide for Cocoa.
NSDictionary
is “toll-free bridged” with its Core Foundation counterpart, CFDictionary Reference. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSDictionary *
parameter, you can pass in a CFDictionaryRef
, and where you see a CFDictionaryRef
parameter, you can pass in an NSDictionary
instance (you cast one type to the other to suppress compiler warnings). This bridging also applies to concrete subclasses of NSDictionary
. See Interchangeable Data Types for more information on toll-free bridging.
+ dictionary
+ dictionaryWithContentsOfFile:
+ dictionaryWithContentsOfURL:
+ dictionaryWithDictionary:
+ dictionaryWithObject:forKey:
+ dictionaryWithObjects:forKeys:
+ dictionaryWithObjects:forKeys:count:
+ dictionaryWithObjectsAndKeys:
– initWithContentsOfFile:
– initWithContentsOfURL:
– initWithDictionary:
– initWithDictionary:copyItems:
– initWithObjects:forKeys:
– initWithObjects:forKeys:count:
– initWithObjectsAndKeys:
– allKeys
– allKeysForObject:
– allValues
– getObjects:andKeys:
– keyEnumerator
– keysSortedByValueUsingSelector:
– objectEnumerator
– objectForKey:
– objectsForKeys:notFoundMarker:
– valueForKey:
– fileCreationDate
– fileExtensionHidden
– fileGroupOwnerAccountID
– fileGroupOwnerAccountName
– fileHFSCreatorCode
– fileHFSTypeCode
– fileIsAppendOnly
– fileIsImmutable
– fileModificationDate
– fileOwnerAccountID
– fileOwnerAccountName
– filePosixPermissions
– fileSize
– fileSystemFileNumber
– fileSystemNumber
– fileType
– description
– descriptionInStringsFileFormat
– descriptionWithLocale:
– descriptionWithLocale:indent:
Creates and returns an empty dictionary.
+ (id)dictionary
A new empty dictionary.
This method is declared primarily for use with mutable subclasses of NSDictionary
.
If you don’t want a temporary object, you can also create an empty dictionary using alloc...
and init
.
NSDictionary.h
Creates and returns a dictionary using the keys and values found in a file specified by a given path.
+ (id)dictionaryWithContentsOfFile:(NSString *)path
A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary. The dictionary must contain only property list objects (instances of NSData
, NSDate
, NSNumber
, NSString
, NSArray
, or NSDictionary
). For more details, see Property List Programming Guide.
A new dictionary that contains the dictionary at path, or nil
if there is a file error or if the contents of the file are an invalid representation of a dictionary.
NSDictionary.h
Creates and returns a dictionary using the keys and values found in a resource specified by a given URL.
+ (id)dictionaryWithContentsOfURL:(NSURL *)aURL
An URL that identifies a resource containing a string representation of a property list whose root object is a dictionary. The dictionary must contain only property list objects (instances of NSData
, NSDate
, NSNumber
, NSString
, NSArray
, or NSDictionary
). For more details, see Property List Programming Guide.
A new dictionary that contains the dictionary at aURL, or nil
if there is an error or if the contents of the resource are an invalid representation of a dictionary.
NSDictionary.h
Creates and returns a dictionary containing the keys and values from another given dictionary.
+ (id)dictionaryWithDictionary:(NSDictionary *)otherDictionary
A dictionary containing keys and values for the new dictionary.
A new dictionary containing the keys and values found in otherDictionary.
NSDictionary.h
Creates and returns a dictionary containing a given key and value.
+ (id)dictionaryWithObject:(id)anObject forKey:(id)aKey
The value corresponding to aKey.
The key for anObject.
A new dictionary containing a single object, anObject, for a single key, aKey.
+ dictionaryWithObjects:forKeys:
+ dictionaryWithObjects:forKeys:count:
+ dictionaryWithObjectsAndKeys:
NSDictionary.h
Creates and returns a dictionary containing entries constructed from the contents of an array of keys and an array of values.
+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
An array containing the values for the new dictionary.
An array containing the keys for the new dictionary. Each key is copied (using copyWithZone:
; keys must conform to the NSCopying
protocol), and the copy is added to the dictionary.
A new dictionary containing entries constructed from the contents of objects and keys.
This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. An NSInvalidArgumentException
is raised if objects and keys don’t have the same number of elements.
– initWithObjects:forKeys:
+ dictionaryWithObject:forKey:
+ dictionaryWithObjects:forKeys:count:
+ dictionaryWithObjectsAndKeys:
NSDictionary.h
Creates and returns a dictionary containing count objects from the objects array.
+ (id)dictionaryWithObjects:(id *)objects forKeys:(id *)keys count:(NSUInteger)count
A C array of values for the new dictionary.
A C array of keys for the new dictionary. Each key is copied (using copyWithZone:
; keys must conform to the NSCopying
protocol), and the copy is added to the new dictionary.
The number of elements to use from the keys and objects arrays. count must not exceed the number of elements in objects or keys.
This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. An NSInvalidArgumentException
is raised if a key or value object is nil
.
The following code fragment illustrates how to create a dictionary that associates the alphabetic characters with their ASCII values:
static const NSInteger N_ENTRIES = 26; |
NSDictionary *asciiDict; |
NSString *keyArray[N_ENTRIES]; |
NSNumber *valueArray[N_ENTRIES]; |
NSInteger i; |
for (i = 0; i < N_ENTRIES; i++) { |
char charValue = 'a' + i; |
keyArray[i] = [NSString stringWithFormat:@"%c", charValue]; |
valueArray[i] = [NSNumber numberWithChar:charValue]; |
} |
asciiDict = [NSDictionary dictionaryWithObjects:(id *)valueArray |
forKeys:(id *)keyArray count:N_ENTRIES]; |
– initWithObjects:forKeys:count:
+ dictionaryWithObject:forKey:
+ dictionaryWithObjects:forKeys:
+ dictionaryWithObjectsAndKeys:
NSDictionary.h
Creates and returns a dictionary containing entries constructed from the specified set of values and keys.
+ (id)dictionaryWithObjectsAndKeys:(id)firstObject , ...
The first value to add to the new dictionary.
First the key for firstObject, then a null-terminated list of alternating values and keys. If any key is nil
, an NSInvalidArgumentException
is raised.
This method is similar to dictionaryWithObjects:forKeys:
, differing only in the way key-value pairs are specified.
For example:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: |
@"value1", @"key1", @"value2", @"key2", nil]; |
– initWithObjectsAndKeys:
+ dictionaryWithObject:forKey:
+ dictionaryWithObjects:forKeys:
+ dictionaryWithObjects:forKeys:count:
NSDictionary.h
Returns a new array containing the receiver’s keys.
- (NSArray *)allKeys
A new array containing the receiver’s keys, or an empty array if the receiver has no entries.
The order of the elements in the array is not defined.
NSDictionary.h
Returns a new array containing the keys corresponding to all occurrences of a given object in the receiver.
- (NSArray *)allKeysForObject:(id)anObject
The value to look for in the receiver.
A new array containing the keys corresponding to all occurrences of anObject in the receiver. If no object matching anObject is found, returns an empty array.
Each object in the receiver is sent an isEqual:
message to determine if it’s equal to anObject.
NSDictionary.h
Returns a new array containing the receiver’s values.
- (NSArray *)allValues
A new array containing the receiver’s values, or an empty array if the receiver has no entries.
The order of the values in the array isn’t defined.
NSDictionary.h
Returns the number of entries in the receiver.
- (NSUInteger)count
The number of entries in the receiver.
NSDictionary.h
Returns a string that represents the contents of the receiver, formatted as a property list.
- (NSString *)description
A string that represents the contents of the receiver, formatted as a property list.
If each key in the receiver is an NSString
object, the entries are listed in ascending order by key, otherwise the order in which the entries are listed is undefined. This method is intended to produce readable output for debugging purposes, not for serializing data. If you want to store dictionary data for later retrieval, see Property List Programming Guide and Archives and Serializations Programming Guide for Cocoa.
NSDictionary.h
Returns a string that represents the contents of the receiver, formatted in .strings
file format.
- (NSString *)descriptionInStringsFileFormat
A string that represents the contents of the receiver, formatted in .strings
file format.
The order in which the entries are listed is undefined.
NSDictionary.h
Returns a string object that represents the contents of the receiver, formatted as a property list.
- (NSString *)descriptionWithLocale:(id)locale
An object that specifies options used for formatting each of the receiver’s keys and values; pass nil
if you don’t want them formatted.
Prior to Mac OS X v10.5, locale must be an instance of NSDictionary
. With Mac OS X v10.5 and later, it may also be an NSLocale
object.
For a description of how locale is applied to each element in the receiver, see descriptionWithLocale:indent:
.
If each key in the dictionary responds to compare:
, the entries are listed in ascending order by key, otherwise the order in which the entries are listed is undefined.
NSDictionary.h
Returns a string object that represents the contents of the receiver, formatted as a property list.
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
An object that specifies options used for formatting each of the receiver’s keys and values; pass nil
if you don’t want them formatted.
Prior to Mac OS X v10.5, locale must be an instance of NSDictionary
. With Mac OS X v10.5 and later, it may also be an NSLocale
object.
Specifies a level of indent, to make the output more readable: set level to 0
to use four spaces to indent, or 1
to indent the output with a tab character
A string object that represents the contents of the receiver, formatted as a property list.
The returned NSString
object contains the string representations of each of the receiver’s entries. descriptionWithLocale:indent:
obtains the string representation of a given key or value as follows:
If the object is an NSString
object, it is used as is.
If the object responds to descriptionWithLocale:indent:
, that method is invoked to obtain the object’s string representation.
If the object responds to descriptionWithLocale:
, that method is invoked to obtain the object’s string representation.
If none of the above conditions is met, the object’s string representation is obtained by invoking its description
method.
If each key in the dictionary responds to compare:
, the entries are listed in ascending order, by key. Otherwise, the order in which the entries are listed is undefined.
NSDictionary.h
Returns the value for the NSFileCreationDate
key.
- (NSDate *)fileCreationDate
The value for the NSFileCreationDate
key, or nil
if the receiver doesn’t have an entry for the key.
NSFileManager.h
Returns the value for the NSFileExtensionHidden
key.
- (BOOL)fileExtensionHidden
The value for the NSFileExtensionHidden
key, or NO
if the receiver doesn’t have an entry for the key.
NSFileManager.h
Returns the value for the NSFileGroupOwnerAccountID
key.
- (NSNumber *)fileGroupOwnerAccountID
The value for the NSFileGroupOwnerAccountID
key, or nil
if the receiver doesn’t have an entry for the key.
NSFileManager.h
Returns the value for the NSFileGroupOwnerAccountName
key.
- (NSString *)fileGroupOwnerAccountName
The value for the key NSFileGroupOwnerAccountName
, or nil
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the name of the corresponding file’s group.
NSFileManager.h
Returns the value for the NSFileHFSCreatorCode
key.
- (OSType)fileHFSCreatorCode
The value for the NSFileHFSCreatorCode
key, or 0
if the receiver doesn’t have an entry for the key.
See HFS File Types for details on the OSType data type.
NSFileManager.h
Returns the value for the NSFileHFSTypeCode
key.
- (OSType)fileHFSTypeCode
The value for the NSFileHFSTypeCode
key, or 0
if the receiver doesn’t have an entry for the key.
See HFS File Types for details on the OSType data type.
NSFileManager.h
Returns the value for the NSFileAppendOnly
key.
- (BOOL)fileIsAppendOnly
The value for the NSFileAppendOnly
key, or NO
if the receiver doesn’t have an entry for the key.
NSFileManager.h
Returns the value for the NSFileImmutable
key.
- (BOOL)fileIsImmutable
The value for the NSFileImmutable
key, or NO
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory.
NSFileManager.h
Returns the value for the key NSFileModificationDate
.
- (NSDate *)fileModificationDate
The value for the key NSFileModificationDate
, or nil
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the date that the file’s data was last modified.
NSFileManager.h
Returns the value for the NSFileOwnerAccountID
key.
- (NSNumber *)fileOwnerAccountID
The value for the NSFileOwnerAccountID
key, or nil
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the account name of the file’s owner.
NSFileManager.h
Returns the value for the key NSFileOwnerAccountName
.
- (NSString *)fileOwnerAccountName
The value for the key NSFileOwnerAccountName
, or nil
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the account name of the file’s owner.
NSFileManager.h
Returns the value for the key NSFilePosixPermissions
.
- (NSUInteger)filePosixPermissions
The value, as an unsigned long
, for the key NSFilePosixPermissions
, or 0
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the file’s permissions.
NSFileManager.h
Returns the value for the key NSFileSize
.
- (unsigned long long)fileSize
The value, as an unsigned long long
, for the key NSFileSize
, or 0
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary such, as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the file’s size.
If the file has a resource fork, the returned value does not include the size of the resource fork.
NSFileManager.h
Returns the value for the key NSFileSystemFileNumber
.
- (NSUInteger)fileSystemFileNumber
The value, as an unsigned long
, for the key NSFileSystemFileNumber
, or 0
if the receiver doesn’t have an entry for the key
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the file’s inode.
NSFileManager.h
Returns the value for the key NSFileSystemNumber
.
- (NSInteger)fileSystemNumber
The value, as an unsigned long
, for the key NSFileSystemNumber
, or 0
if the receiver doesn’t have an entry for the key
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the ID of the device containing the file.
NSFileManager.h
Returns the value for the key NSFileType
.
- (NSString *)fileType
The value for the key NSFileType
, or nil
if the receiver doesn’t have an entry for the key.
This and the other file...
methods are for use with a dictionary, such as those returned from the methods fileAttributesAtPath:traverseLink:
(NSFileManager
), directoryAttributes
(NSDirectoryEnumerator
), and fileAttributes
(NSDirectoryEnumerator
), that represents the POSIX attributes of a file or directory. This method returns the file’s type. Possible return values are described in the “Constants” section of NSFileManager
.
NSFileManager.h
Returns by reference C arrays of the keys and values in the receiver.
- (void)getObjects:(id *)objects andKeys:(id *)keys
Upon return, contains a C array of the values in the receiver.
Upon return, contains a C array of the keys in the receiver.
The elements in the returned arrays are ordered such that the first element in objects is the value for the first key in keys and so on.
NSDictionary.h
Initializes a newly allocated dictionary using the keys and values found in a file at a given path.
- (id)initWithContentsOfFile:(NSString *)path
A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary. The dictionary must contain only property list objects (instances of NSData
, NSDate
, NSNumber
, NSString
, NSArray
, or NSDictionary
). For more details, see Property List Programming Guide.
An initialized object—which might be different than the original receiver—that contains the dictionary at path, or nil
if there is a file error or if the contents of the file are an invalid representation of a dictionary.
NSDictionary.h
Initializes a newly allocated dictionary using the keys and values found at a given URL.
- (id)initWithContentsOfURL:(NSURL *)aURL
An URL that identifies a resource containing a string representation of a property list whose root object is a dictionary. The dictionary must contain only property list objects (instances of NSData
, NSDate
, NSNumber
, NSString
, NSArray
, or NSDictionary
). For more details, see Property List Programming Guide.
An initialized object—which might be different than the original receiver—that contains the dictionary at aURL, or nil
if there is an error or if the contents of the resource are an invalid representation of a dictionary.
NSDictionary.h
Initializes a newly allocated dictionary by placing in it the keys and values contained in another given dictionary.
- (id)initWithDictionary:(NSDictionary *)otherDictionary
A dictionary containing keys and values for the new dictionary.
An initialized object—which might be different than the original receiver—containing the keys and values found in otherDictionary.
NSDictionary.h
Initializes a newly allocated dictionary using the objects contained in another given dictionary.
- (id)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag
A dictionary containing keys and values for the new dictionary.
A flag that specifies whether values in otherDictionary should be copied. If YES
, the members of otherDictionary are copied, and the copies are added to the receiver. If NO
, the values of otherDictionary are retained by the new dictionary.
An initialized object—which might be different than the original receiver—containing the keys and values found in otherDictionary.
Note that copyWithZone:
is used to make copies. Thus, the receiver’s new member objects may be immutable, even though their counterparts in otherDictionary were mutable. Also, members must conform to the NSCopying
protocol.
NSDictionary.h
Initializes a newly allocated dictionary with entries constructed from the contents of the objects and keys arrays.
- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
An array containing the values for the new dictionary.
An array containing the keys for the new dictionary. Each key is copied (using copyWithZone:
; keys must conform to the NSCopying
protocol), and the copy is added to the new dictionary.
This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. An NSInvalidArgumentException
is raised if the objects and keys arrays do not have the same number of elements.
NSDictionary.h
Initializes a newly allocated dictionary with count entries.
- (id)initWithObjects:(id *)objects forKeys:(id *)keys count:(NSUInteger)count
A C array of values for the new dictionary.
A C array of keys for the new dictionary. Each key is copied (using copyWithZone:
; keys must conform to the NSCopying
protocol), and the copy is added to the new dictionary.
The number of elements to use from the keys and objects arrays. count must not exceed the number of elements in objects or keys.
This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. An NSInvalidArgumentException
is raised if a key or value object is nil
.
NSDictionary.h
Initializes a newly allocated dictionary with entries constructed from the specified set of values and keys.
- (id)initWithObjectsAndKeys:(id)firstObject , ...
The first value to add to the new dictionary.
First the key for firstObject, then a null-terminated list of alternating values and keys. If any key is nil
, an NSInvalidArgumentException
is raised.
This method is similar to initWithObjects:forKeys:
, differing only in the way in which the key-value pairs are specified.
For example:
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: |
@"value1", @"key1", @"value2", @"key2", nil]; |
NSDictionary.h
Returns a Boolean value that indicates whether the contents of the receiver are equal to the contents of another given dictionary.
- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary
The dictionary with which to compare the receiver.
YES
if the contents of otherDictionary are equal to the contents of the receiver, otherwise NO
.
Two dictionaries have equal contents if they each hold the same number of entries and, for a given key, the corresponding value objects in each dictionary satisfy the isEqual:
test.
– isEqual:
(NSObject
protocol)NSDictionary.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 this method.
NSEnumerator *enumerator = [myDictionary keyEnumerator]; |
id key; |
while ((key = [enumerator nextObject])) { |
/* code that uses the returned key */ |
} |
If you use this method with instances of mutable subclasses of NSDictionary
, your code should not modify the entries during enumeration. If you intend to modify the entries, use the allKeys
method to create a “snapshot” of the dictionary’s keys. Then use this snapshot to traverse the entries, modifying them along the way.
Note that the objectEnumerator
method provides a convenient way to access each value in the dictionary.
NSDictionary.h
Returns an array of the receiver’s keys, in the order they would be in if the receiver were sorted by its values.
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator
A selector that specifies the method to use to compare the values in the receiver.
The comparator method should return NSOrderedAscending
if the receiver is smaller than the argument, NSOrderedDescending
if the receiver is larger than the argument, and NSOrderedSame
if they are equal.
An array of the receiver’s keys, in the order they would be in if the receiver were sorted by its values.
Pairs of dictionary values are compared using the comparison method specified by comparator; the comparator message is sent to one of the values and has as its single argument the other value from the dictionary.
– allKeys
– sortedArrayUsingSelector:
(NSArray
)NSDictionary.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 = [myDictionary objectEnumerator]; |
id value; |
while ((value = [enumerator nextObject])) { |
/* code that acts on the dictionary’s values */ |
} |
If you use this method with instances of mutable subclasses of NSDictionary
, your code should not modify the entries during enumeration. If you intend to modify the entries, use the allValues
method to create a “snapshot” of the dictionary’s values. Work from this snapshot to modify the values.
– keyEnumerator
– nextObject
(NSEnumerator
)NSDictionary.h
Returns 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.
NSDictionary.h
Returns the set of objects from the receiver that corresponds to the specified keys as an NSArray.
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)anObject
The keys for which to return corresponding values.
The marker object to place in the corresponding element of the returned array if an object isn’t found in the receiver to correspond to a given key.
The objects in the returned array and the keys array have a one-for-one correspondence, so that the nth object in the returned array corresponds to the nth key in keys.
NSDictionary.h
Returns the value associated with a given key.
- (id)valueForKey:(NSString *)key
The key for which to return the corresponding value. Note that when using key-value coding, the key must be a string (see Key-Value Coding Fundamentals).
The value associated with key.
If key does not start with “@
”, invokes objectForKey:
. If key does start with “@
”, strips the “@” and invokes [super valueForKey:]
with the rest of the key.
– setValue:forKey:
(NSMutableDictionary
)– getObjects:andKeys:
NSKeyValueCoding.h
Writes a property list representation of the contents of the receiver to a given path.
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
The path at which to write the file.
If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath
before invoking this method.
A flag that specifies whether the file should be written atomically.
If flag is YES
, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. If flag is NO
, the dictionary is written directly to path. The YES
option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.
YES
if the file is written successfully, otherwise NO
.
This method recursively validates that all the contained objects are property list objects (instances of NSData
, NSDate
, NSNumber
, NSString
, NSArray
, or NSDictionary
) before writing out the file, and returns NO
if all the objects are not property list objects, since the resultant file would not be a valid property list.
If the receiver’s contents are all property list objects, the file written by this method can be used to initialize a new dictionary with the class method dictionaryWithContentsOfFile:
or the instance method initWithContentsOfFile:
.
For more information about property lists, see Property List Programming Guide.
NSDictionary.h
Writes a property list representation of the contents of the receiver to a given URL.
- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag
The URL to which to write the receiver.
A flag that specifies whether the output should be written atomically.
If flag is YES
, the receiver is written to an auxiliary location, and then the auxiliary location is renamed to aURL. If flag is NO
, the dictionary is written directly to aURL. The YES
option guarantees that aURL, if it exists at all, won’t be corrupted even if the system should crash during writing. flag is ignored if aURL is of a type that cannot be written atomically.
YES
if the location is written successfully, otherwise NO
.
This method recursively validates that all the contained objects are property list objects (instances of NSData
, NSDate
, NSNumber
, NSString
, NSArray
, or NSDictionary
) before writing out the file, and returns NO
if all the objects are not property list objects, since the resultant output would not be a valid property list.
If the receiver’s contents are all property list objects, the location written by this method can be used to initialize a new dictionary with the class method dictionaryWithContentsOfURL:
or the instance method initWithContentsOfURL:
.
For more information about property lists, see Property List Programming Guide.
NSDictionary.h
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-04-08)