Dictionary

struct Dictionary<Key : Hashable, Value>

A hash-based mapping from Key to Value instances. Also a collection of key-value pairs with no defined ordering.

Inheritance CollectionType, DebugPrintable, DictionaryLiteralConvertible, Printable, Reflectable, SequenceType, _CollectionType, _SequenceType, _Sequence_Type View Protocol Hierarchy →
Associated Types
Element = (Key, Value)
Index = DictionaryIndex<Key, Value>
Generator = DictionaryGenerator<Key, Value>

Type alias inferred.

SubSequence = Slice<Dictionary<Key, Value>>

Type alias inferred.

Import import Swift

Initializers

init()

Create an empty dictionary.

Declaration

init()
init(dictionaryLiteral:)

Create an instance initialized with elements.

Declaration

init(dictionaryLiteral elements: (Key, Value)...)
init(minimumCapacity:)

Create a dictionary with at least the given number of elements worth of storage. The actual capacity will be the smallest power of 2 that's >= minimumCapacity.

Declaration

init(minimumCapacity: Int)

Instance Variables

var count: Int

The number of entries in the dictionary.

Complexity: O(1)

Declaration

var count: Int { get }
var debugDescription: String

A textual representation of self, suitable for debugging.

Declaration

var debugDescription: String { get }
var description: String

A textual representation of self.

Declaration

var description: String { get }
var endIndex: DictionaryIndex<Key, Value>

The collection's "past the end" position.

endIndex is not a valid argument to subscript, and is always reachable from startIndex by zero or more applications of successor().

Complexity: amortized O(1) if self does not wrap a bridged NSDictionary, O(N) otherwise.

Declaration

var endIndex: DictionaryIndex<Key, Value> { get }
var isEmpty: Bool

True iff count == 0

Declaration

var isEmpty: Bool { get }
var keys: LazyForwardCollection<MapCollectionView<[Key : Value], Key>>

A collection containing just the keys of self

Keys appear in the same order as they occur as the .0 member of key-value pairs in self. Each key in the result has a unique value.

Declaration

var keys: LazyForwardCollection<MapCollectionView<[Key : Value], Key>> { get }
var startIndex: DictionaryIndex<Key, Value>

The position of the first element in a non-empty dictionary.

Identical to endIndex in an empty dictionary

Complexity: amortized O(1) if self does not wrap a bridged NSDictionary, O(N) otherwise.

Declaration

var startIndex: DictionaryIndex<Key, Value> { get }
var values: LazyForwardCollection<MapCollectionView<[Key : Value], Value>>

A collection containing just the values of self

Values appear in the same order as they occur as the .1 member of key-value pairs in self.

Declaration

var values: LazyForwardCollection<MapCollectionView<[Key : Value], Value>> { get }

Subscripts

subscript(_: Key)

Declaration

subscript(key: Key) -> Value?
subscript(_: DictionaryIndex<Key, Value>)

Declaration

subscript(position: DictionaryIndex<Key, Value>) -> (Key, Value) { get }

Instance Methods

func generate()

Return a generator over the (key, value) pairs.

Complexity: O(1)

Declaration

func generate() -> DictionaryGenerator<Key, Value>
func getMirror()

Returns a mirror that reflects self.

Declaration

func getMirror() -> MirrorType
func indexForKey(_:)

Returns the Index for the given key, or nil if the key is not present in the dictionary.

Declaration

func indexForKey(key: Key) -> DictionaryIndex<Key, Value>?
mutating func removeAll(_:)

Remove all elements.

Postcondition: capacity == 0 if keepCapacity is false, otherwise the capacity will not be decreased.

Invalidates all indices with respect to self.

keepCapacity If true, the operation preserves the storage capacity that the collection has, otherwise the underlying storage is released. The default is false.

Complexity: O(count).

Declaration

mutating func removeAll(keepCapacity: Bool = default)
mutating func removeAtIndex(_:)

Remove the key-value pair at index i

Invalidates all indices with respect to self.

Complexity: O(count).

Declaration

mutating func removeAtIndex(index: DictionaryIndex<Key, Value>)
mutating func removeValueForKey(_:)

Remove a given key and the associated value from the dictionary. Returns the value that was removed, or nil if the key was not present in the dictionary.

Declaration

mutating func removeValueForKey(key: Key) -> Value?
mutating func updateValue(_:forKey:)

Update the value stored in the dictionary for the given key, or, if they key does not exist, add a new key-value pair to the dictionary.

Returns the value that was replaced, or nil if a new key-value pair was added.

Declaration

mutating func updateValue(value: Value, forKey key: Key) -> Value?