We take it for granted these days but it was a huge quality of life improvement over the syntax we previously had.
But it’s also an important performance improvement because the alternative was to write string joining code like this.
String interpolation hasn’t changed much since Swift 1.0, with the only real change coming in Swift 2.1 where we gained the ability to use string literals in interpolations, like this.
- print("Hi, \(user ?? "Anonymous")")
As for Swift 5.0, it's fair to say that ABI stability is the star of the show - it's what developers are most keen to see. But there's so much more, not the least of which are raw strings, Result, and isMultiple(of:).
Well, after five years of hard service, Swift evolution has finally come for string interpolation: In Swift 5.0, it gains new superpowers that give us substantially more control over how it works.
If we make an age Integer like this,
I can use that with string interpolation.
- let age = 25
- print("HI, I'm \(age).")
- OUTPUT : HI, I'm 25.
Using the new String interpolation system in Swift 5.0, we can extend String.StringInterpolation to add our own custom interpolation like this.
- extension String.StringInterpolation {
- mutating func appendInterpolation(_ value: Int) {
- let formatter = NumberFormatter()
- formatter.numberStyle = .spellOut
- if let result = formatter.string(from: value as NSNumber) {
- appendLiteral(result)
- }
- }
- }
- print("HI, I'am \(age).")
- OUTPUT : HI, I'am twenty-five.
We could use the same technique to adjust date formatting because, by default, dates in strings don't look great.
- print("Today's date is \(Date()).")
- OUTPUT : Today's date is 2019-05-28 11:26:18 +0000.
You'll see that Swift prints the date as something like "2019-05-28 11:26:18 +0000".
Now, you can make a customized date like this:
- extension String.StringInterpolation {
- mutating func appendInterpolation(_ value: Date) {
- let formatter = DateFormatter()
- formatter.dateStyle = .full
- let dateString = formatter.string(from: value)
- appendLiteral(dateString)
- }
- }
- print("Today's date is \(Date()).")
- OUTPUT : Today's date is Tuesday, May 28, 2019.
Interpolation with parameters
The above example shows how we have complete control over the way string interpolation handles parameters. You see appendInterpolation() so that we can handle various different data types in unique ways.
For example, we could write some code to handle Twitter handles.
- extension String.StringInterpolation {
- mutating func appendInterpolation(twitter: String) {
- appendLiteral("<a href=\"https://twitter.com/\(twitter)\">@\(twitter)</a>")
- }
- }
- print("You should follow me on Twitter: \(twitter: "praveshdubey99").")
- OUTPUT : You should follow me on Twitter: <a href="https://twitter.com/praveshdubey99">@praveshdubey99</a>.
We could use that inside the method rather than forcing a spell out style.
- extension String.StringInterpolation {
- mutating func appendInterpolation(format value: Int, using style: NumberFormatter.Style) {
- let formatter = NumberFormatter()
- formatter.numberStyle = style
- if let result = formatter.string(from: value as NSNumber) {
- appendLiteral(result)
- }
- }
- }
- print("Hi, I'm \(format: age, using: .spellOut).")
- OUTPUT : Hi, I'm twenty-five.