Swift 4 – Top 3 new String API features

October 13, 2017
Posted in iOS
October 13, 2017 mihailsalari

With each release, Apple makes us love Swift more and more. But, it wasn’t always like that. For me personally, Swift 3 felt to be the first official Swift release, and not the versions 1 and 2. They felt more like beta releases of the language. Now we get Swift 4 which is an upgrade of Swift 3 and not a complete revamp of the language like in the previous versions. It comes with the new Xcode 9. After playing around with Swift 4, I have decided to dedicate a story which will focus on the new exciting String API.

About Swift 4

Swift 4 builds on the strengths of Swift 3, delivering greater robustness and stability, providing source code compatibility with Swift 3, making improvements to the standard library, and adding features like smart key paths and serialization, all while shortening build times and reducing the size of app binaries.

String API

String is one of the most used classes in Swift, so finally, it received a lot of well-deserved love. The new API contains a lot of changes, but I will focus only on the best ones.

 

#3 Multi-Line String Literals

It will make you forget about adding new lines with inline \n on a long string and make your strings looking dirty and hard to read. This is one of the many examples how multi-line strings literals can be helpful.
Let’s compare the current and the new string multi-line approach by adding an example string into print()

Swift 3

print("\nExample:\n- first line\n- second line\n- third line")

Swift 4

print("""Example:
- first line
- second line
- third line""")

Probably you can spot the difference and how clean the new multi-line string is. In order to use multi-line string literals, you will need to use “”” to determine the base indentation of the string.

#2 Collections

Strings are collections once again. It was introduced first in Swift 1 but was removed later in Swift 2. The string is now treated as a collection of characters and we can perform various operations on them (iteration, filter etc.).

Iterate directly over String

let str = "Awesome String"
for char in str{
  print(char)
}

Use filter on String

let str = "Awesome String"
let filteredString = string.filter { $0 != "a" }

#1 Substring Type

Swift 4 adds a new type used for referencing a subsequence on String, called Substring. They are both implementing StringProtocol which gives them almost identical functionality. Let’s see few examples how we can use the new Substring type.

Split a String into Substring

let str = "Awesome String"
let endIndex = str.index(str.startIndex, offsetBy: 4)
var awesoSubstr = str[str.startIndex...endIndex]   // "Aweso" // awesoSubstr is now a Substring type

Create a String from Substring 

let awesoStr = String(awesoSubstr) // "Aweso"

Concatenate a String into Substring

awesoSubstr += "me" //Awesome

These are just a few of the many new features that the String API offers and you can read about them in the StringManifesto.

Conclusion

The String API which can be found in 100% of the apps that are available on the market received some deserved love from its creators and made great updates. I am sure that I will use these new features in every possible situation and will remove a lot of struggle and frustration from the past.

I think Apple is finally leading Swift in a right direction and instead of revamping it they have decided to strengthen the weak spots and make a more powerful and more stable programming language. I am not speaking this only about the String API, but also in all the other new features that are coming with Swift 4.

,

Creative design and development from KIV

Get in touch with me!