What’s New in Swift 5

Breakdown of the new features available in Swift 5, which Apple released in the latest Xcode 10.2 beta version.

Mateusz Szklarek
EL Passion Blog
4 min readFeb 20, 2019

--

New Xcode 10.2 comes to us with Swift 5, which is a significant change for all Apple developers. One of the most important aspects of the new version of Swift is ABI stability which enables binary compatibility between applications and libraries compiled with different versions of Swift. I will go through some new features and bug fixes included in the newest version of the Apple language.

What’s new?

@dynamicCallable attribute

First, I should explain what gives us interoperability to dynamic languages like Python, JavaScript, Perl and Ruby (introduced with @dynamicMemberLookup feature in Swift 4.2).

Let’s analyze the code below, written in python:

If we want to use the above code in Swift it would look like this:

What gives us @dynamicCallable attribute? It’s just syntactic sugar for current usage but looks more natural and clear.

Key paths supports the identity keypath (\.self)

Enumeration case now supports array instead of variadic

Flatten nested optionals when using ‘try ?’

First, we should look at the Swift built-in mechanisms which allow us to avoid accidentally creating nested optionals, for example:

as?

optional chain

It’s quite easy to get nested Optional (??) when using “try?” in Swift 4.2.1. In Swift 5 it’s been changed.

try?

Literal initialization via coercion

If a type T conforms to one of the protocols in Initialization with Literals — such as ExpressibleByIntegerLiteral — and literal is a literal expression, then T (literal) creates a literal of type T using the corresponding protocol, rather than calling an initializer of T with a value of the protocol’s default literal type.

If you want to see the details click here:

String interpolation has improved performance, clarity, and efficiency

I have done performance tests in the command line which compare execution times of basic operations on a string —the final result is an average from 5 executions (each was executed 100k times).

Let’s visualize our data in a basic chart.

Chart which shows comparison execution times of different operations on a string in Swift 4.2.1 & 5

String concatenation’s execution time, basically, doesn’t change, but string interpolation is almost 2 times faster, and concatenation using joined (separator:) function is almost 2.5 times faster!

Swift 3 mode has been removed. Supported versions: 4, 4.2 and 5.

@autoclosure parameters

@autoclosure parameters can no longer be forwarded to @autoclosure arguments in another function call. Instead, you must explicitly call the function value with parentheses: ();

Casting an optional value to a generic type

Casting an optional value to a generic placeholder type, the compiler will be more conservative with the unwrapping of the value. The result of such a cast now more closely matches the result you would get in a nongeneric context.

Protocols can now constrain their conforming types in equivalent forms

didSet bug

When setting a property from within its own didSet or willSet observer, the observer will now only avoid being recursively called if the property is set on self (either implicitly or explicitly).

To better understand what changed under the hood, run the below script using Swift 4.2.1 and then the Swift 5 version:

Here are the results:

We can see that in Swift 4.2.1 ‘grandchild’ doesn’t get updated and in Swift 5 it works as it should.

Let’s sum up!

Swift is constantly evolving, which is normal for a modern language, but it’s good to see that Swift is finally becoming stable. ABI stability, which enables binary compatibility between applications and libraries compiled with different versions of Swift makes our lives easier. It’s a nice change that in the newest version Swift, the standard library won’t be included in the app, which decreases the final size of the app. I’m really looking forward to a stable version of Swift 5 ❤️.

Tap the 👏 button if you found this article useful!

About the Author
Mateusz is a member of one of the best iOS teams in Warsaw. He loves unit testing and believes that “each line of code matters”. You can find him on GitHub or Twitter.

Find EL Passion on Facebook, Twitter, and Instagram.

--

--

Mateusz Szklarek
EL Passion Blog

He is a member of one of the best iOS teams in Warsaw. He loves unit testing and believes that “each line of code matters”.