An object representing a static collection of key-value pairs, for use instead of a Dictionary
constant in cases that require reference semantics.
SDKs
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
class NSDictionary : NSObject
Overview
The NSDictionary
class declares the programmatic interface to objects that manage immutable associations of keys and values. For example, an interactive form could be represented as a dictionary, with the field names as keys, corresponding to user-entered values.
Use this class or its subclass NSMutable
when you need a convenient and efficient way to retrieve data associated with an arbitrary key. NSDictionary
creates static dictionaries, and NSMutable
creates dynamic dictionaries. (For convenience, the term dictionary refers 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 is
). 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 Accessing Object Properties). Neither a key nor a value can be nil
; if you need to represent a null value in a dictionary, you should use NSNull
.
NSDictionary
is “toll-free bridged” with its Core Foundation counterpart, CFDictionary
. See Toll-Free Bridging for more information on toll-free bridging.
Creating NSDictionary Objects Using Dictionary Literals
In addition to the provided initializers, such as init(objects:
, you can create an NSDictionary
object using a dictionary literal.
let dictionary: NSDictionary = [
"anObject" : someObject,
"helloString" : "Hello, World!",
"magicNumber" : 42,
"aValue" : someValue
]
In Objective-C, the compiler generates code that makes an underlying call to the init(objects:
method.
id objects[] = { someObject, @"Hello, World!", @42, someValue };
id keys[] = { @"anObject", @"helloString", @"magicNumber", @"aValue" };
NSUInteger count = sizeof(objects) / sizeof(id);
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys
count:count];
Unlike dictionary
and other initializers, dictionary literals specify entries in key-value order. You should not terminate the list of objects with nil
when using this literal syntax, and in fact nil
is an invalid value. For more information about object literals in Objective-C, see Working with Objects in Programming with Objective-C.
In Swift, the NSDictionary
class conforms to the DictionaryLiteralConvertible protocol, which allows it to be initialized with dictionary literals. For more information about object literals in Swift, see Literal Expression in The Swift Programming Language (Swift 4.1).
Accessing Values Using Subscripting
In addition to the provided instance methods, such as object(for
, you can access NSDictionary
values by their keys using subscripting.
let value = dictionary["helloString"]
Enumerating Entries Using for-in Loops
In addition to the provided instance methods, such as enumerate
, you can enumerate NSDictionary
entries using for-in loops.
for (key, value) in dictionary {
print("Value: \(value) for key: \(key)")
}
In Objective-C, NSDictionary
conforms to the NSFast
protocol.
In Swift, NSDictionary
conforms to the SequenceType protocol.
Subclassing Notes
You generally shouldn’t need to subclass NSDictionary
. Custom behavior can usually be achieved through composition rather than subclassing.
Methods to Override
If you do need to subclass NSDictionary
, take into account that it is a Class cluster. Any subclass must override the following primitive methods:
The other methods of NSDictionary
operate by invoking one or more of these primitives. The non-primitive methods provide convenient ways of accessing multiple entries at once.
Alternatives to Subclassing
Before making a custom class of NSDictionary
, investigate NSMap
and the corresponding Core Foundation type, CFDictionary. Because NSDictionary
and CFDictionary
are “toll-free bridged,” you can substitute a CFDictionary
object for a NSDictionary
object in your code (with appropriate casting). Although they are corresponding types, CFDictionary
and NSDictionary
do not have identical interfaces or implementations, and you can sometimes do things with CFDictionary
that you cannot easily do with NSDictionary
.
If the behavior you want to add supplements that of the existing class, you could write a category on NSDictionary
. Keep in mind, however, that this category will be in effect for all instances of NSDictionary
that you use, and this might have unintended consequences. Alternatively, you could use composition to achieve the desired behavior.