String Interpolation In Swift 5.0

String interpolation has been around since the earliest days of Swift, but in Swift 5.0, it’s getting a massive improvement to make it faster and more powerful.

The Basics

We use basic String interpolation like this.
  1. let age = 25  
  2. print("You are \(age)")  

We take it for granted these days but it was a huge quality of life improvement over the syntax we previously had.

  1. [NSString stringWithFormat:@"%ld", (long)unreadCount];  

But it’s also an important performance improvement because the alternative was to write string joining code like this.

  1. let all = s1 + s2 + s3 + s4  

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.

  1. 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,
  1. let age = 25  
I can use that with string interpolation.
  1. let age = 25  
  2. print("HI, I'm \(age).")  
  3. 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.
  1. extension String.StringInterpolation {  
  2.     mutating func appendInterpolation(_ value: Int) {  
  3.         let formatter = NumberFormatter()  
  4.         formatter.numberStyle = .spellOut  
  5.         if let result = formatter.string(from: value as NSNumber) {  
  6.             appendLiteral(result)  
  7.         }  
  8.     }  
  9. }  
  10. print("HI, I'am \(age).")
  11. OUTPUTHI, I'am twenty-five.
We could use the same technique to adjust date formatting because, by default, dates in strings don't look great.
  1. print("Today's date is \(Date()).")  
  2. 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:
  1. extension String.StringInterpolation {  
  2.     mutating func appendInterpolation(_ value: Date) {  
  3.         let formatter = DateFormatter()  
  4.         formatter.dateStyle = .full  
  5.         let dateString = formatter.string(from: value)  
  6.         appendLiteral(dateString)  
  7.     }  
  8. }  
  9. print("Today's date is \(Date()).")  
  10. 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.
  1. extension String.StringInterpolation {  
  2.     mutating func appendInterpolation(twitter: String) {  
  3.         appendLiteral("<a href=\"\(twitter)\">@\(twitter)</a>")  
  4.     }  
  5. }  
  6. print("You should follow me on Twitter: \(twitter: "praveshdubey99").")  
  7. OUTPUT : You should follow me on Twitter: <a href="">@praveshdubey99</a>.
We could use that inside the method rather than forcing a spell out style.
  1. extension String.StringInterpolation {  
  2.     mutating func appendInterpolation(format value: Int, using style: NumberFormatter.Style) {  
  3.         let formatter = NumberFormatter()  
  4.         formatter.numberStyle = style  
  5.         if let result = formatter.string(from: value as NSNumber) {  
  6.             appendLiteral(result)  
  7.         }  
  8.     }  
  9. }  
  10. print("Hi, I'm \(format: age, using: .spellOut).")
  11. OUTPUT : Hi, I'm twenty-five.