The Power of String Interpolation in Swift
Exploring String Interpolations new superpowers in Swift
We Speak Strings, We Write Strings
Strings are everywhere in our world. We developers start our day with strings, writing code in our favorite IDE and end with strings, writing our status emails.
It doesn’t matter whether we want to or not, we’re writing lots of string interpolation every day. We have no other option.
Perhaps you’re now thinking, when did I use string interpolation? I don’t think I’ve ever used it!
Well, perhaps you’ve failed to spot it, but trust me — you’re using it daily!
If I am using it daily, then why do I need to learn it now?
Many programming languages in the world support string interpolation. Swift has supported string interpolation from its inception. But in Swift 5.0 it got superpowers, which why I’m writing about it and exploring some of the new features of string interpolation. Let’s do it!
What is String Interpolation?
String interpolation is including a variable or constant in our string. It is a way of building a new string by mixing up constants, variables, and literals with their values, inside a string literal.
Let’s say I want to say “good morning” to “boudhayan”, how do I do it?
We all knew that — we’re using this type of code every day. It’s very simple.
String Interpolation is just a fancy name for a well-known functionality
Now let’s check the Swift code to see how has it been defined in Swift Standard library.
It’s just a simple type alias of
So what’s new with String Interpolation in Swift 5?
Now we can extend String Interpolation to include our own custom interpolation. We can use string interpolation as we want now — there are no restrictions!
But what does that actually mean? Let’s create two simple custom interpolations and see what can we do with them.
Here we’ve created two extensions of
There are two steps to follow when creating a custom interpolation:
- Create an extension of
- Write custom implementation for
mutating func appnedInterpolation()
Let’s implement the actual method. We have to give a custom implementation to the
appendInterpolation method. This method is doing all the magic for us, so be sure about what are we’re trying to achieve and give the implementation according to that.
In the first extension, we want the string interpolation to spell all the integer values for us so we don't need to write more code. In the second extension it’s doing exactly the same as the first but with
This is our custom string interpolation. As you can see, it is spelling out the
date in the terminal.
Yes, this is the superpower of String Interpolation we were talking about!
And we can achieve many other things with string interpolation.
For example, we can take closure as a parameter. In the code snippet below, we’ve implemented one custom string interpolation function, which takes one
autoclosure closure and one string literal as a parameter. The
condition closure returns
Bool , if the closure returns
true then only it appends the string literal with string, otherwise no interpolation happens.
age is greater than
18, it only prints the
Let’s take another example. In the code snippet below
appendInterpolation function takes one
String and if the
empty it prints the
default message; otherwise it joins the
array elements with
-> and prints.
Let’s say we have an
optional string that we want to print in the console. But whenever we write it inside
optional. We don’t want this — we want to print the exact value of that
There are two options available to us. One is to force unwrapping the
string optional. The other is to give it a
default value by using the
?? operator. We can do the same thing but in a more verbose way.
Let’s make it simple by using string interpolation. We can implement a String Interpolation function like below, which takes one
one optional string and one
default value. It prints the
default value if the
optional string contains
nil value, otherwise it unwraps the
value optional string and prints the wrapped value.
Up to this point, we’ve seen the scope for implementing a custom string interpolation method. We can do limitless things — we can play with this. We can create our own custom type that uses string interpolation, this literally shows the superpower of string interpolation. We’ll create a
ConsoleMessage type that can be initialized using interpolation and prints different messages in predefined interpolated formats for different types of
Here’s the implementation:
We’ve implemented many custom string interpolation functions in this article. The surprising fact is that this is not the limit — we can do so many other cool things with it for fun and serious use cases also.
Why wait? Go and do it now!
Thank you for reading!