Contents

NSDictionary

A static collection of objects associated with unique keys.

Declaration

class NSDictionary

Mentioned in

Overview

You can use this type in Swift instead of a Dictionary in cases that require reference semantics.

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 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, 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 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 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:forKeys:), you can create an NSDictionary object using a dictionary literal.

In Objective-C, the compiler generates code that makes an underlying call to the dictionaryWithObjects:forKeys:count: 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 dictionaryWithObjectsAndKeys: 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(forKey:), you can access NSDictionary values by their keys using subscripting.

Enumerating Entries Using for-in Loops

In addition to the provided instance methods, such as enumerateKeysAndObjects(_:), you can enumerate NSDictionary entries using for-in loops.

In Objective-C, NSDictionary conforms to the NSFastEnumeration 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 NSMapTable 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.

Topics

Creating an Empty Dictionary

Creating a Dictionary from Objects and Keys

Creating a Dictionary from Another Dictionary

Creating a Dictionary from an External Source

Creating a Dictionary from an NSCoder

Creating Key Sets for Shared-Key Optimized Dictionaries

Counting Entries

Comparing Dictionaries

Accessing Keys and Values

Enumerating Dictionaries

Sorting Dictionaries

Filtering Dictionaries

Storing Dictionaries

Accessing File Attributes

Describing a Dictionary

Initializers

Default Implementations