protocol
ExpressibleByDictionaryLiteral
Inheritance | View Protocol Hierarchy → |
---|---|
Associated Types |
The key type of a dictionary literal.
The value type of a dictionary literal. |
Import |
|
Initializers
Creates an instance initialized with the given key-value pairs.
Declaration
init
(
dictionaryLiteral
elements
: (
Self
.
Key
,
Self
.
Value
)...)
A type that can be initialized using a dictionary literal.
A dictionary literal is a simple way of writing a list of key-value pairs. You write each key-value pair with a colon (
:
) separating the key and the value. The dictionary literal is made up of one or more key-value pairs, separated by commas and surrounded with square brackets.To declare a dictionary, assign a dictionary literal to a variable or constant:
When the context provides enough type information, you can use a special form of the dictionary literal, square brackets surrounding a single colon, to initialize an empty dictionary.
Note: A dictionary literal is not the same as an instance of
Dictionary
or the similarly namedDictionaryLiteral
type. You can't initialize a type that conforms toExpressibleByDictionaryLiteral
simply by assigning an instance of one of these types.Conforming to the ExpressibleByDictionaryLiteral Protocol
To add the capability to be initialized with a dictionary literal to your own custom types, declare an
init(dictionaryLiteral:)
initializer. The following example shows the dictionary literal initializer for a hypotheticalCountedSet
type, which uses setlike semantics while keeping track of the count for duplicate elements:n
: The new count forelement
.n
must be greater /// than or equal to zero. ///element
: The element to set the new count on. mutating func updateCount(_ n: Int, for element: Element) }