protocol
ExpressibleByStringInterpolation
A type that can be initialized by string interpolation with a string literal that includes expressions.
Inheritance | ExpressibleByStringLiteral |
---|---|
Conforming Types | StringProtocol |
Associated Types |
|
Use string interpolation to include one or more expressions in a string literal, wrapped in a set of parentheses and prefixed by a backslash. For example:
let
price
=
2
let
number
=
3
let
message
=
"One cookie: $\(
price
), \(
number
) cookies: $\(
price
*
number
)."
(
message
)
// Prints "One cookie: $2, 3 cookies: $6."
Extending the Default Interpolation Behavior
Add new interpolation behavior to existing types by extending
DefaultStringInterpolation
, the type that implements interpolation for
types like String
and Substring
, to add an overload of
appendInterpolation(_:)
with their new behavior.
For more information, see the DefaultStringInterpolation
and
StringInterpolationProtocol
documentation.
Creating a Type That Supports the Default String Interpolation
To create a new type that supports string literals and interpolation, but
that doesn't need any custom behavior, conform the type to
ExpressibleByStringInterpolation
and implement the
init(stringLiteral: String)
initializer declared by the
ExpressibleByStringLiteral
protocol. Swift will automatically use
DefaultStringInterpolation
as the interpolation type and provide an
implementation for init(stringInterpolation:)
that passes the
interpolated literal's contents to init(stringLiteral:)
, so you don't
need to implement anything specific to this protocol.
Creating a Type That Supports Custom String Interpolation
If you want a conforming type to differentiate between literal and
interpolated segments, restrict the types that can be interpolated,
support different interpolators from the ones on String
, or avoid
constructing a String
containing the data, the type must specify a custom
StringInterpolation
associated type. This type must conform to
StringInterpolationProtocol
and have a matching StringLiteralType
.
For more information, see the StringInterpolationProtocol
documentation.
Initializers
Creates an instance from a string interpolation.
Most StringInterpolation
types will store information about the
literals and interpolations appended to them in one or more properties.
init(stringInterpolation:)
should use these properties to initialize
the instance.
- Parameter stringInterpolation: An instance of
StringInterpolation
which has had each segment of the string literal appended to it.
Declaration
init
(
stringInterpolation
:
Self
.
StringInterpolation
)
The
StringLiteralType
of an interpolation type must match theStringLiteralType
of the conforming type.