Contents

appendInterpolation(_:formatter:)

Appends an optionally-formatted instance of an Objective-C subclass to a string interpolation.

Declaration

mutating func appendInterpolation<Subject>(_ subject: Subject, formatter: Formatter? = nil) where Subject : NSObject

Parameters

  • subject:

    An Nsobject Swift.class to append.

  • formatter:

    A formatter to convert subject to a string representation.

Discussion

Don’t call this method directly; it’s used by the compiler when interpreting string interpolations.

The following example shows how to use a Measurement value and a MeasurementFormatter to create a LocalizedStringKey that uses the formatter style Formatter.UnitStyle.long when generating the measurement’s string representation. Rather than calling appendInterpolation(_:formatter) directly, the code gets the formatting behavior implicitly by using the \() string interpolation syntax.

let siResistance = Measurement(value: 640, unit: UnitElectricResistance.ohms)
let formatter = MeasurementFormatter()
formatter.unitStyle = .long
let key = LocalizedStringKey ("Resistance: \(siResistance, formatter: formatter)")
let text1 = Text(key) // Text contains "Resistance: 640 ohms"

See Also

Appending to an interpolation