Protocolswift 6.0.1Swift
ExpressibleByStringInterpolation
A type that can be initialized by string interpolation with a string literal that includes expressions.
protocol ExpressibleByStringInterpolation : ExpressibleByStringLiteral
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)."
print(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.
Supertypes
protocol ExpressibleByExtendedGraphemeClusterLiteral
A type that can be initialized with a string literal containing a single extended grapheme cluster.
protocol ExpressibleByStringLiteral
A type that can be initialized with a string literal.
protocol ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single Unicode scalar value.
Requirements
Type members
associatedtype StringInterpolation
The type each segment of a string literal containing interpolations should be appended to.
init(stringInterpolation: Self.StringInterpolation
) Creates an instance from a string interpolation.
Citizens in Swift
Subtypes
protocol StringProtocol
A type that can represent a string as a collection of characters.
Citizens in Swift
where Self.StringInterpolation == DefaultStringInterpolation
Type members
init(stringInterpolation: DefaultStringInterpolation
) Creates a new instance from an interpolated string literal.