StringInterpolationConvertible

protocol StringInterpolationConvertible

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:

let price = 2
let number = 3
let message = "One cookie: $\(price), \(number) cookies: $\(price * number)."
print(message)
// Prints "One cookie: $2, 3 cookies: $6."

Conforming to the StringInterpolationConvertible Protocol

To use string interpolation to initialize instances of your custom type, implement the required initializers for StringInterpolationConvertible conformance. String interpolation is a multiple-step initialization process. When you use string interpolation, the following steps occur:

  1. The string literal is broken into pieces. Each segment of the string literal before, between, and after any included expressions, along with the individual expressions themselves, are passed to the init(stringInterpolationSegment:) initializer.
  2. The results of those calls are passed to the 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:

let message = String(stringInterpolation:
      String(stringInterpolationSegment: "One cookie: $"),
      String(stringInterpolationSegment: price),
      String(stringInterpolationSegment: ", "),
      String(stringInterpolationSegment: number),
      String(stringInterpolationSegment: " cookies: $"),
      String(stringInterpolationSegment: price * number),
      String(stringInterpolationSegment: "."))
Inheritance View Protocol Hierarchy →
Import import Swift

Initializers

init(stringInterpolation:) Required

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)"
print(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...)
init(stringInterpolationSegment:) Required

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)"
print(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)