DefaultStringInterpolation

struct DefaultStringInterpolation

Represents a string literal with interpolations while it is being built up.

Inheritance StringInterpolationProtocol, CustomStringConvertible, TextOutputStream

Do not create an instance of this type directly. It is used by the compiler when you create a string using string interpolation. Instead, use string interpolation to create a new string by including values, literals, variables, or expressions enclosed in parentheses, prefixed by a backslash (\(...)).

let price = 2
let number = 3
let message = """
              If one cookie costs \(price) dollars, \
              \(number) cookies cost \(price * number) dollars.
              """
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

When implementing an ExpressibleByStringInterpolation conformance, set the StringInterpolation associated type to DefaultStringInterpolation to get the same interpolation behavior as Swift's built-in String type and construct a String with the results. If you don't want the default behavior or don't want to construct a String, use a custom type conforming to StringInterpolationProtocol instead.

Extending default string interpolation behavior

Code outside the standard library can extend string interpolation on String and many other common types by extending DefaultStringInterpolation and adding an appendInterpolation(...) method. For example:

extension DefaultStringInterpolation {
    fileprivate mutating func appendInterpolation(
             escaped value: String, asASCII forceASCII: Bool = false) {
        for char in value.unicodeScalars {
            appendInterpolation(char.escaped(asASCII: forceASCII))
        }
    }
}

print("Escaped string: \(escaped: string)")

See StringInterpolationProtocol for details on appendInterpolation methods.

DefaultStringInterpolation extensions should add only mutating members and should not copy self or capture it in an escaping closure.

Initializers

init init(literalCapacity:interpolationCount:) Required

Creates a string interpolation with storage pre-sized for a literal with the indicated attributes.

Do not call this initializer directly. It is used by the compiler when interpreting string interpolations.

Declaration

@inlinable public init(literalCapacity: Int, interpolationCount: Int)

Instance Variables

var description Required

A textual representation of this instance.

Calling this property directly is discouraged. Instead, convert an instance of any type to a string by using the String(describing:) initializer. This initializer works with any type, and uses the custom description property for types that conform to CustomStringConvertible:

struct Point: CustomStringConvertible {
    let x: Int, y: Int

    var description: String {
        return "(\(x), \(y))"
    }
}

let p = Point(x: 21, y: 30)
let s = String(describing: p)
print(s)
// Prints "(21, 30)"

The conversion of p to a string in the assignment to s uses the Point type's description property.

Declaration

var description: String

Instance Methods

func appendInterpolation(_ value: T) Required

Interpolates the given value's textual representation into the string literal being created.

Do not call this method directly. It is used by the compiler when interpreting string interpolations. Instead, use string interpolation to create a new string by including values, literals, variables, or expressions enclosed in parentheses, prefixed by a backslash (\(...)).

let price = 2
let number = 3
let message = """
              If one cookie costs \(price) dollars, \
              \(number) cookies cost \(price * number) dollars.
              """
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

Declaration

@inlinable public mutating func appendInterpolation<T>(_ value: T) where T: CustomStringConvertible, T: TextOutputStreamable
func appendInterpolation(_ value: T) Required

Interpolates the given value's textual representation into the string literal being created.

Do not call this method directly. It is used by the compiler when interpreting string interpolations. Instead, use string interpolation to create a new string by including values, literals, variables, or expressions enclosed in parentheses, prefixed by a backslash (\(...)).

let price = 2
let number = 3
let message = "If one cookie costs \(price) dollars, " +
              "\(number) cookies cost \(price * number) dollars."
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

Declaration

@inlinable public mutating func appendInterpolation<T>(_ value: T) where T: TextOutputStreamable
func appendInterpolation(_ value: T) Required

Interpolates the given value's textual representation into the string literal being created.

Do not call this method directly. It is used by the compiler when interpreting string interpolations. Instead, use string interpolation to create a new string by including values, literals, variables, or expressions enclosed in parentheses, prefixed by a backslash (\(...)).

let price = 2
let number = 3
let message = """
              If one cookie costs \(price) dollars, \
              \(number) cookies cost \(price * number) dollars.
              """
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

Declaration

@inlinable public mutating func appendInterpolation<T>(_ value: T) where T: CustomStringConvertible
func appendInterpolation(_ value: T) Required

Interpolates the given value's textual representation into the string literal being created.

Do not call this method directly. It is used by the compiler when interpreting string interpolations. Instead, use string interpolation to create a new string by including values, literals, variables, or expressions enclosed in parentheses, prefixed by a backslash (\(...)).

let price = 2
let number = 3
let message = """
              If one cookie costs \(price) dollars, \
              \(number) cookies cost \(price * number) dollars.
              """
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

Declaration

@inlinable public mutating func appendInterpolation<T>(_ value: T)
func appendLiteral(_ literal: String) Required

Appends a literal segment of a string interpolation.

Do not call this method directly. It is used by the compiler when interpreting string interpolations.

Declaration

@inlinable public mutating func appendLiteral(_ literal: String)
func write(_ string: String) Required

Appends the given string to the stream.

Declaration

@inlinable public mutating func write(_ string: String)