Class: NSDictionary
Overview
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. NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (For convenience, we use the term dictionary to refer to any instance of one of these classes without specifying its exact class membership.)
Direct Known Subclasses
Class Method Summary (collapse)
-
+ dictionary
Creates and returns an empty dictionary.
-
+ dictionaryWithContentsOfFile:
Creates and returns a dictionary using the keys and values found in a file specified by a given path.
-
+ dictionaryWithContentsOfURL:
Creates and returns a dictionary using the keys and values found in a resource specified by a given URL.
-
+ dictionaryWithDictionary:
Creates and returns a dictionary containing the keys and values from another given dictionary.
-
+ dictionaryWithObject:forKey:
Creates and returns a dictionary containing a given key and value.
-
+ dictionaryWithObjects:forKeys:
Creates and returns a dictionary containing entries constructed from the contents of an array of keys and an array of values.
-
+ dictionaryWithObjects:forKeys:count:
Creates and returns a dictionary containing count objects from the objects array.
-
+ dictionaryWithObjectsAndKeys:
Creates and returns a dictionary containing entries constructed from the specified set of values and keys.
-
+ sharedKeySetForKeys:
Creates a shared key set object for the specified keys.
Instance Method Summary (collapse)
-
- allKeys
Returns a new array containing the dictionary’s keys.
-
- allKeysForObject:
Returns a new array containing the keys corresponding to all occurrences of a given object in the dictionary.
-
- allValues
Returns a new array containing the dictionary’s values.
-
- count
Returns the number of entries in the dictionary.
-
- description
Returns a string that represents the contents of the dictionary, formatted as a property list.
-
- descriptionInStringsFileFormat
Returns a string that represents the contents of the dictionary, formatted in .strings file format.
-
- descriptionWithLocale:
Returns a string object that represents the contents of the dictionary, formatted as a property list.
-
- descriptionWithLocale:indent:
Returns a string object that represents the contents of the dictionary, formatted as a property list.
-
- enumerateKeysAndObjectsUsingBlock:
Applies a given block object to the entries of the dictionary.
-
- enumerateKeysAndObjectsWithOptions:usingBlock:
Applies a given block object to the entries of the dictionary.
-
- fileCreationDate
Returns the value for the NSFileCreationDate key.
-
- fileExtensionHidden
Returns the value for the NSFileExtensionHidden key.
-
- fileGroupOwnerAccountID
Returns the value for the NSFileGroupOwnerAccountID key.
-
- fileGroupOwnerAccountName
Returns the value for the NSFileGroupOwnerAccountName key.
-
- fileHFSCreatorCode
Returns the value for the NSFileHFSCreatorCode key.
-
- fileHFSTypeCode
Returns the value for the NSFileHFSTypeCode key.
-
- fileIsAppendOnly
Returns the value for the NSFileAppendOnly key.
-
- fileIsImmutable
Returns the value for the NSFileImmutable key.
-
- fileModificationDate
Returns the value for the key NSFileModificationDate.
-
- fileOwnerAccountID
Returns the value for the NSFileOwnerAccountID key.
-
- fileOwnerAccountName
Returns the value for the key NSFileOwnerAccountName.
-
- filePosixPermissions
Returns the value for the key NSFilePosixPermissions.
-
- fileSize
Returns the value for the key NSFileSize.
-
- fileSystemFileNumber
Returns the value for the key NSFileSystemFileNumber.
-
- fileSystemNumber
Returns the value for the key NSFileSystemNumber.
-
- fileType
Returns the value for the key NSFileType.
-
- getObjects:andKeys:
Returns by reference C arrays of the keys and values in the dictionary.
-
- initWithContentsOfFile:
Initializes a newly allocated dictionary using the keys and values found in a file at a given path.
-
- initWithContentsOfURL:
Initializes a newly allocated dictionary using the keys and values found at a given URL.
-
- initWithDictionary:
Initializes a newly allocated dictionary by placing in it the keys and values contained in another given dictionary.
-
- initWithDictionary:copyItems:
Initializes a newly allocated dictionary using the objects contained in another given dictionary.
-
- initWithObjects:forKeys:
Initializes a newly allocated dictionary with entries constructed from the contents of the objects and keys arrays.
-
- initWithObjects:forKeys:count:
Initializes a newly allocated dictionary with count entries.
-
- initWithObjectsAndKeys:
Initializes a newly allocated dictionary with entries constructed from the specified set of values and keys.
-
- isEqualToDictionary:
Returns a Boolean value that indicates whether the contents of the receiving dictionary are equal to the contents of another given dictionary.
-
- keyEnumerator
Returns an enumerator object that lets you access each key in the dictionary.
-
- keysOfEntriesPassingTest:
Returns the set of keys whose corresponding value satisfies a constraint described by a block object.
-
- keysOfEntriesWithOptions:passingTest:
Returns the set of keys whose corresponding value satisfies a constraint described by a block object.
-
- keysSortedByValueUsingComparator:
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values using a given comparator block.
-
- keysSortedByValueUsingSelector:
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values.
-
- keysSortedByValueWithOptions:usingComparator:
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values using a given comparator block and a specified set of options.
-
- objectEnumerator
Returns an enumerator object that lets you access each value in the dictionary.
-
- objectForKey:
Returns the value associated with a given key.
-
- objectForKeyedSubscript:
Returns the value associated with a given key.
-
- objectsForKeys:notFoundMarker:
Returns the set of objects from the dictionary that corresponds to the specified keys as an NSArray.
-
- valueForKey:
Returns the value associated with a given key.
-
- writeToFile:atomically:
Writes a property list representation of the contents of the dictionary to a given path.
-
- writeToURL:atomically:
Writes a property list representation of the contents of the dictionary to a given URL.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (Object) dictionary
Creates and returns an 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.
+ (Object) dictionaryWithContentsOfFile(path)
Creates and returns a dictionary using the keys and values found in a file specified by a given path. The dictionary representation in the file identified by path must contain only property list objects (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
+ (Object) dictionaryWithContentsOfURL(aURL)
Creates and returns a dictionary using the keys and values found in a resource specified by a given URL. The dictionary representation in the file identified by path must contain only property list objects (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
+ (Object) dictionaryWithDictionary(otherDictionary)
Creates and returns a dictionary containing the keys and values from another given dictionary.
+ (Object) dictionaryWithObject(anObject, forKey:aKey)
Creates and returns a dictionary containing a given key and value.
+ (Object) dictionaryWithObjects(objects, forKeys:keys)
Creates and returns a dictionary containing entries constructed from the contents of an array of keys and an array of values. 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.
+ (Object) dictionaryWithObjects(objects, forKeys:keys, count:count)
Creates and returns a dictionary containing count objects from the objects array. 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:
+ (Object) dictionaryWithObjectsAndKeys(firstObject, ...)
Creates and returns a dictionary containing entries constructed from the specified set of values and keys. This method is similar to dictionaryWithObjects:forKeys:, differing only in the way key-value pairs are specified.For example:
+ (Object) sharedKeySetForKeys(keys)
Creates a shared key set object for the specified keys. The array of keys may contain duplicates which are quietly ignored. Duplicate hash values of the keys are quietly allowed, but may cause lower performance and increase memory usage.Typically you would create a shared key set for a given set of keys once, before creating shared key dictionaries, and retain and save the result of this method for use with the NSMutableDictionary class method dictionaryWithSharedKeySet:.
Instance Method Details
- (Array) allKeys
Returns a new array containing the dictionary’s keys. The order of the elements in the array is not defined.
- (Array) allKeysForObject(anObject)
Returns a new array containing the keys corresponding to all occurrences of a given object in the dictionary. Each object in the dictionary is sent an isEqual: message to determine if it’s equal to anObject.
- (Array) allValues
Returns a new array containing the dictionary’s values. The order of the values in the array isn’t defined.
- (Integer) count
Returns the number of entries in the dictionary.
- (String) description
Returns a string that represents the contents of the dictionary, formatted as a property list. If each key in the dictionary 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.
- (String) descriptionInStringsFileFormat
Returns a string that represents the contents of the dictionary, formatted in .strings file format. The order in which the entries are listed is undefined.
- (String) descriptionWithLocale(locale)
Returns a string object that represents the contents of the dictionary, formatted as a property list. For a description of how locale is applied to each element in the dictionary, 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.
- (String) descriptionWithLocale(locale, indent:level)
Returns a string object that represents the contents of the dictionary, formatted as a property list. The returned NSString object contains the string representations of each of the dictionary’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.
- (Object) enumerateKeysAndObjectsUsingBlock(block)
Applies a given block object to the entries of the dictionary. If the block sets *stop to YES, the enumeration stops.
- (Object) enumerateKeysAndObjectsWithOptions(opts, usingBlock:block)
Applies a given block object to the entries of the dictionary. If the block sets *stop to YES, the enumeration stops.
- (NSDate) fileCreationDate
Returns the value for the NSFileCreationDate key.
- (Boolean) fileExtensionHidden
Returns the value for the NSFileExtensionHidden key.
- (NSNumber) fileGroupOwnerAccountID
Returns the value for the NSFileGroupOwnerAccountID key.
- (String) fileGroupOwnerAccountName
Returns the value for the NSFileGroupOwnerAccountName 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.
- (OSType) fileHFSCreatorCode
Returns the value for the NSFileHFSCreatorCode key. See “HFS File Types” for details on the OSType data type.
- (OSType) fileHFSTypeCode
Returns the value for the NSFileHFSTypeCode key. See “HFS File Types” for details on the OSType data type.
- (Boolean) fileIsAppendOnly
Returns the value for the NSFileAppendOnly key.
- (Boolean) fileIsImmutable
Returns the value for the NSFileImmutable 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.
- (NSDate) fileModificationDate
Returns the value for the key NSFileModificationDate. 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.
- (NSNumber) fileOwnerAccountID
Returns the value for the NSFileOwnerAccountID 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.
- (String) fileOwnerAccountName
Returns the value for the key NSFileOwnerAccountName. 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.
- (Integer) filePosixPermissions
Returns the value for the key NSFilePosixPermissions. 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.
- (Integer) fileSize
Returns the value for the key NSFileSize. 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.
- (Integer) fileSystemFileNumber
Returns the value for the key NSFileSystemFileNumber. 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.
- (Integer) fileSystemNumber
Returns the value for the key NSFileSystemNumber. 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.
- (String) fileType
Returns the value for the key NSFileType. 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.
- (Object) getObjects(objects, andKeys:keys)
Returns by reference C arrays of the keys and values in the dictionary. 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.
- (Object) initWithContentsOfFile(path)
Initializes a newly allocated dictionary using the keys and values found in a file at a given path. The dictionary representation in the file identified by path must contain only property list objects (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
- (Object) initWithContentsOfURL(aURL)
Initializes a newly allocated dictionary using the keys and values found at a given URL. The dictionary representation in the file identified by path must contain only property list objects (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
- (Object) initWithDictionary(otherDictionary)
Initializes a newly allocated dictionary by placing in it the keys and values contained in another given dictionary.
- (Object) initWithDictionary(otherDictionary, copyItems:flag)
Initializes a newly allocated dictionary using the objects contained in another given dictionary. After an immutable dictionary has been initialized in this way, it cannot be modified.The copyWithZone: method performs a shallow copy. If you have a collection of arbitrary depth, passing YES for the flag parameter will perform an immutable copy of the first level below the surface. If you pass NO the mutability of the first level is unaffected. In either case, the mutability of all deeper levels is unaffected.
- (Object) initWithObjects(objects, forKeys:keys)
Initializes a newly allocated dictionary with entries constructed from the contents of the objects and keys arrays. 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.
- (Object) initWithObjects(objects, forKeys:keys, count:count)
Initializes a newly allocated dictionary with count entries. 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.
- (Object) initWithObjectsAndKeys(firstObject, ...)
Initializes a newly allocated dictionary with entries constructed from the specified set of values and keys. This method is similar to initWithObjects:forKeys:, differing only in the way in which the key-value pairs are specified.For example:
- (Boolean) isEqualToDictionary(otherDictionary)
Returns a Boolean value that indicates whether the contents of the receiving dictionary are equal to the contents of another given dictionary. 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.
- (NSEnumerator) keyEnumerator
Returns an enumerator object that lets you access each key in the dictionary. The following code fragment illustrates how you might use this method. 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.
- (NSSet) keysOfEntriesPassingTest(predicate)
Returns the set of keys whose corresponding value satisfies a constraint described by a block object.
- (NSSet) keysOfEntriesWithOptions(opts, passingTest:predicate)
Returns the set of keys whose corresponding value satisfies a constraint described by a block object.
- (Array) keysSortedByValueUsingComparator(cmptr)
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values using a given comparator block.
- (Array) keysSortedByValueUsingSelector(comparator)
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary 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.
- (Array) keysSortedByValueWithOptions(opts, usingComparator:cmptr)
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values using a given comparator block and a specified set of options.
- (NSEnumerator) objectEnumerator
Returns an enumerator object that lets you access each value in the dictionary. The following code fragment illustrates how you might use the method. 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.
- (Object) objectForKey(aKey)
Returns the value associated with a given key.
- (Object) objectForKeyedSubscript(key)
Returns the value associated with a given key. This method is the same as valueForKey:.
- (Array) objectsForKeys(keys, notFoundMarker:anObject)
Returns the set of objects from the dictionary that corresponds to the specified keys as an NSArray. The objects in the returned array and the keys array have a one-for-one correspondence, so that the nthe object in the returned array corresponds to the nthe key in keys.
- (Object) valueForKey(key)
Returns the value associated with a given 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.
- (Boolean) writeToFile(path, atomically:flag)
Writes a property list representation of the contents of the dictionary to a given path. 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 dictionary’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.
- (Boolean) writeToURL(aURL, atomically:flag)
Writes a property list representation of the contents of the dictionary to a given URL. 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 dictionary’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.