protocol
ExpressibleByStringInterpolation
Inheritance | View Protocol Hierarchy → |
---|---|
Import |
|
Initializers
Creates an instance by concatenating the given values.
Do not call this initializer directly. It is used by the compiler when you use string interpolation. For example:
let
s
=
"\(
5
) x \(
2
) = \(
5
*
2
)"
(
s
)
// Prints "5 x 2 = 10"
After calling init(stringInterpolationSegment:)
with each segment of
the string literal, this initializer is called with their string
representations.
strings
: An array of instances of the conforming type.
Declaration
init
(
stringInterpolation
strings
:
Self
...)
Creates an instance containing the appropriate representation for the given value.
Do not call this initializer directly. It is used by the compiler for each string interpolation segment when you use string interpolation. For example:
let
s
=
"\(
5
) x \(
2
) = \(
5
*
2
)"
(
s
)
// Prints "5 x 2 = 10"
This initializer is called five times when processing the string literal
in the example above; once each for the following: the integer 5
, the
string " x "
, the integer 2
, the string " = "
, and the result of
the expression 5 * 2
.
expr
: The expression to represent.
Declaration
init
<
T
>
(
stringInterpolationSegment
expr
:
T
)
A type that can be initialized by string interpolation with a string literal that includes expressions.
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:
Conforming to the ExpressibleByStringInterpolation Protocol
To use string interpolation to initialize instances of your custom type, implement the required initializers for
ExpressibleByStringInterpolation
conformance. String interpolation is a multiple-step initialization process. When you use string interpolation, the following steps occur:init(stringInterpolationSegment:)
initializer.init(stringInterpolation:)
initializer in the order in which they appear in the string literal.In other words, initializing the
message
constant in the example above using string interpolation is equivalent to the following code:Deprecated: it will be replaced or redesigned in Swift 4.0. Instead of conforming to 'ExpressibleByStringInterpolation', consider adding an 'init(_:String)'.