Dictionary
extension Dictionary
-
Creates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.
// [Int64: [Player]] let dictionary = try Dictionary(grouping: cursor, by: { $0.teamID })
Declaration
Swift
public init<C: Cursor>(grouping cursor: C, by keyForValue: (C.Element) throws -> Key) throws where Value == [C.Element]
Parameters
cursor
A cursor of values to group into a dictionary.
keyForValue
A closure that returns a key for each element in the cursor.
-
Creates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.
// [Int64: [Player]] let dictionary = try Dictionary( minimumCapacity: 100, grouping: cursor, by: { $0.teamID })
Declaration
Swift
public init<C: Cursor>(minimumCapacity: Int, grouping cursor: C, by keyForValue: (C.Element) throws -> Key) throws where Value == [C.Element]
Parameters
minimumCapacity
The minimum number of key-value pairs that the newly created dictionary should be able to store without reallocating its storage buffer.
cursor
A cursor of values to group into a dictionary.
keyForValue
A closure that returns a key for each element in the cursor.
-
Creates a new dictionary from the key-value pairs in the given cursor.
You use this initializer to create a dictionary when you have a cursor of key-value tuples with unique keys. Passing a cursor with duplicate keys to this initializer results in a fatal error.
// [Int64: Player] let playerByID = try Dictionary(uniqueKeysWithValues: cursor)
Precondition
The cursor must not have duplicate keys.Declaration
Swift
public init<C: Cursor>(uniqueKeysWithValues keysAndValues: C) throws where C.Element == (Key, Value)
Parameters
keysAndValues
A cursor of key-value pairs to use for the new dictionary. Every key in
keysAndValues
must be unique. -
Creates a new dictionary from the key-value pairs in the given cursor.
You use this initializer to create a dictionary when you have a cursor of key-value tuples with unique keys. Passing a cursor with duplicate keys to this initializer results in a fatal error.
// [Int64: Player] let playerByID = try Dictionary( minimumCapacity: 100, uniqueKeysWithValues: cursor)
Precondition
The cursor must not have duplicate keys.Declaration
Swift
public init<C: Cursor>(minimumCapacity: Int, uniqueKeysWithValues keysAndValues: C) throws where C.Element == (Key, Value)
Parameters
minimumCapacity
The minimum number of key-value pairs that the newly created dictionary should be able to store without reallocating its storage buffer.
keysAndValues
A cursor of key-value pairs to use for the new dictionary. Every key in
keysAndValues
must be unique.