What is New in Swift 5.0?

Swift 5 was announced to be released in early 2019.

The primary focus of Swift 5 will be ABI (Application Binary Interface) stability for the Swift Standard Library. Binary compatibility allows Swift code to be compiled by different Swift compilers and link together, and interoperate at a runtime level.

Swift 5.0 is not binary compatible with earlier Swift releases. However, future Swift releases will be compatible with Swift 5.

Why does ABI Stability matter?

  • Bundle size will be reduced
  • Language changes are smaller / Less frequent
  • Less Migration
  • Developers can create Pre-compiled Frameworks in Swift (currently frameworks are compiled when compiling your app)

Several key new features are already implemented in Swift 5.

SE-0235 A standard Result type

Swift’s Result type is implemented as an enum that has two cases: success and failure. Both are implemented using generics so they can have any associated value, but failure must be something that conforms to Swift’s Error type.

Below is a function that connects to a server to figure out how many unread messages are waiting for the user. It accepts a URL string as its first parameter, and a completion handler with a Result as its second parameter. The success case will store an integer, and the failure case will be an option of NetworkError.

Result type gives the benefit of getting back strongly-typed errors.

Also, it returns back either successful data or an error – it is not possible to get both or neither of them.

SE-0200 Raw strings

Swift 5 has an updated ability to create raw strings, where backslashes and quote marks are interpreted as literal symbols rather than escapes characters or string terminators. It’s achieved by wrapping a string into hash symbols (#).

SE-0228 Customizing string interpolation

The new string interpolation system lets control how objects appear in strings. For this purpose, an extension to String.StringInterpolation with a new appendInterpolation() method should be added.

SE-0216 Dynamically callable types

A new @dynamicCallable attribute was added to Swift, which brings the ability to mark a type as being directly callable. Such Function Objects (Functors)  are used in  C++, Python, JavaScript and other languages. In Swift 5 Functors were introduced for interaction with these languages.

SE-0192 Handling future enum cases

One of Swift’s security features requires all switch statements to be exhaustive – that they must cover all cases. The @unknown attribute added before default case, it provides the ability to get a warning when new cases are added. So all new cases can be handled individually.

In previous Swift versions, all new cases were handled by default automatically.

SE-0230 Flattening nested optionals resulting from try?

Swift 5 modifies the way try? works so that nested optionals are flattened to become regular optionals. This makes it works the same way as optional chaining and conditional typecasts, both of which flatten optionals in earlier Swift versions.

SE-0225 Checking for integer multiples

Introduction of isMultiple(of:) method to integers allows to check whether one number is multiple in a much clearer way than using the division remainder operation (%).

SE-0218 Transforming and unwrapping dictionary values with compactMapValues()

A new compactMapValues() method is added to dictionaries, bringing together the compactMap() functionality from arrays with the mapValues() method from dictionaries. As a result, a new dictionary with transformed non-nil values can be created.

The most recent Swift updates were mentioned in the article. To keep pace with the new iOS tools and technologies, follow our blog!

The article was written by Eugenia Galetskaya