String interpolation with a specifier or formatter?

Does anyone know where to find more detailed information about "String interpolation with a specifier/formatter"?

I know we can use string interpolation to format a number:
Code Block swift
let pi = Double.pi
"\(pi, specifier: "%.2f")"

but I can't find any further information about this syntax, please help.
Answered by OOPer in 626637022

I know we can use string interpolation to format a number:

You know your code causes error under the normal context:
Code Block
let pi = Double.pi
let str = "\(pi, specifier: "%.2f")" //->Extra argument 'specifier' in call



specifier: or other extra arguments are based on this evolution proposal.

Fix ExpressibleByStringInterpolation

When there is a type (not String) which conforms to ExpressibleByStringInterpolation and its associated StringInterpolation has a method appendInterpolation(_:specifier:),
you can use String Interpolation literals like "\(pi, specifier: "%.2f")".

The type LocalizedStringKey is the one that matches this condition. (Which is intended to use with Text of SwiftUI.)

Code Block
import SwiftUI
let pi = Double.pi
let str: LocalizedStringKey = "\(pi, specifier: "%.2f")"


You may need to check the doc of LocalizedStringKey.StringInterpolation, to find what sort of extra arguments you can use.

If you want to use another type conforming to ExpressibleByStringInterpolation, you need to search another place.
Accepted Answer

I know we can use string interpolation to format a number:

You know your code causes error under the normal context:
Code Block
let pi = Double.pi
let str = "\(pi, specifier: "%.2f")" //->Extra argument 'specifier' in call



specifier: or other extra arguments are based on this evolution proposal.

Fix ExpressibleByStringInterpolation

When there is a type (not String) which conforms to ExpressibleByStringInterpolation and its associated StringInterpolation has a method appendInterpolation(_:specifier:),
you can use String Interpolation literals like "\(pi, specifier: "%.2f")".

The type LocalizedStringKey is the one that matches this condition. (Which is intended to use with Text of SwiftUI.)

Code Block
import SwiftUI
let pi = Double.pi
let str: LocalizedStringKey = "\(pi, specifier: "%.2f")"


You may need to check the doc of LocalizedStringKey.StringInterpolation, to find what sort of extra arguments you can use.

If you want to use another type conforming to ExpressibleByStringInterpolation, you need to search another place.
String interpolation with a specifier or formatter?
 
 
Q