New features in Swift 5.1

Image for post
Image for post

With the Beta version of Xcode 11 we now have access to these great new features.

Apple are now creating Swift only frameworks, so the excitement and development around Swift is really pushing forwards so there has never been a better time to either start using Swift or brush up with the fundamentals.

So let's get right in!

Module stability

You can now use third-party libraries without concern for the version of the Swift compiler they were created with. Module stability is similar to ABI stability at the compilation stage rather than at the execution stage. We now have a stable interface between modules.

Shared Swift runtime

Apps now use the runtime from the OS when it is available. If your App works on an older version of iOS the runtime will be bundled with your App. This means that your Apps size will be much smaller than ever before, and this gives a faster runtime for App (which is one of the advantages of iOS12).

Return from single-expression functions without the return keyword

You can now return from single expression functions, and you do not need the return function as it is inferred by the compiler.

Improvements to memberwise initializers for structs

Synthesized memberwise initializers do not have to be used for default parameters!

Universal Self

Note the use of a capital letter in self here.

This is to solve the problem as follows, when we subclass an instance variable. Since we refer to a static instance of number, we return NumberReturns.number.description. Unfortunately the result of the following code unexpectedly returns 9!

Now we can use Self rather than returning to the instance member.

In this case we return 5, which is the expected result.

Static subscripts

Any place where it was previously legal to declare a subscript, it is now legal to declare a static subscript.

The example given in Swift evolution (https://github.com/apple/swift-evolution/blob/master/proposals/0254-static-subscripts.md) is around avoiding singleton instances for environment variables. For my simple example, I just show how static subscripts could be used. Surprisingly, it is just that simple:

difference(from:)

Swift can now calculate the difference between two ordered collections.

this is of particular interest as it can return the offset of the difference, for use in tableviews (for example).

An array initializer with access to uninitialized storage

This allows an array to be created that allows us to access the buffer of unititialized memory so algorithms can be implemented that don’t know the size of the array required in advance — without undergoing the performance penalty of resizing arrays. We do need to be careful about those uninitialised array elements though!

Pattern matching between optionals and non-optionals for enums

This is a long requested feature to bring enums up to the status of strings and integers for the switch/case pattern to match options with non-options in the following style:

Opaque return types

This is about the ability to hide the result type of functions from the caller in public interfaces.

This is wonderful because it allows us to return instances that are hidden from the client, and could change from one version of a library to the next without breaking clients.

A suitable use for this might be in a mutliple choice quiz with multiple types of quizzes.

Want to get in contact? Try the link here:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store