A type that can be initialized by string interpolation with a string literal that includes expressions.
SDK
- Xcode 10.2+
Framework
- Swift Standard Library
Declaration
Overview
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:
Extending the Default Interpolation Behavior
Add new interpolation behavior to existing types by extending Default
, the type that implements interpolation for types like String
and Substring
, to add an overload of append
with their new behavior.
For more information, see the Default
and String
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 Expressible
and implement the init(string
initializer declared by the Expressible
protocol. Swift will automatically use Default
as the interpolation type and provide an implementation for init(string
that passes the interpolated literal’s contents to init(string
, 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 String
associated type. This type must conform to String
and have a matching String
.
For more information, see the String
documentation.