Contents

subscript(_:)

Accesses the value associated with a given key.

Declaration

@objc dynamic subscript(key: Any) -> Any? { get }

Parameters

  • key:

    The key whose value you want to retrieve.

Discussion

The returned value is nil if key does not exist in the dictionary.

The listing below creates a new dictionary. It prints the value of a key found in the dictionary ("Coral") and a key not found in the dictionary ("Cerise").

var hues = NSDictionary(dictionary: ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156])
print(hues["Coral"])
// Prints "Optional(16)"
print(hues["Cerise"])
// Prints "nil"

See Also

Accessing Keys and Values