What is new in Swift 4.2?

At the beginning of June, the largest event for iOS endorsers took place – WWDC18. SoftTeco brings you an overview of what Apple has in store for the developer community.

Several updates for Swift were presented there. Main attention was focused on the following: faster compilation, new language features that improve efficiency and reduce boilerplate. Certainly, SDK improvements for Swift were presented too. Apple promises that Obj-C API will reflect Swift better and the language usage will become easier. Work on binary compatibility is underway for future Swift versions however it’s likely a prospect for Swift 5.

One of the important points that need to be mentioned is the appearance of a new language compatibility mode in the Xcode settings – Swift 4.2. To remind you multiple language compatibility modes in Swift 3 and Swift 4, there is a short description below:

Swift 3 – Accepts Swift 3 code built with Xcode 8

Swift 4 – Accepts Swift 4 code built with Xcode 9.3

Swift 4.2 is similar to Swift 4 but it incorporates new Swift-related SDK improvements.

The compatibility course was taken and the source code becomes closer to the future language versions. This means that SDK changes that usually affect compatibility will be minimized. Worth mentioning, this was the last release with support of Swift 3, so all developers are highly recommended to update their code to Swift 4.2.

Great work was done to make building faster. Apple promises a twofold acceleration for the majority of projects, adjusted for the fact that this value depends on the number of cores and project size. Previously, a lot of unnecessary work was done by the compiler due to the Swift cross-file visibility before. Now, the compilation pipeline is optimized to reduce redundant work across files.

One more feature was added for the Swift compiler – Compilation Mode. Developers had limited options to customize builds before: No optimization – Onone or Optimize for Speed – O. However, improvements are being added. Compilation Mode allows developers to choose which files to compile: project files optimization (what is more effective) or process only necessary changes (that allows making builds faster).

Default settings such as Incremental – for Debug, Whole Module – for Release configuration are most optimal in the majority of cases. Apple has also mentioned that “Whole Module” compilation mode with “No optimization”. Optimization Level sometimes allows making builds faster because it decreases the amount of redundant work for the compiler.

Moreover, one more optimization mode has arisen – Optimize for Size.

It will allow making compression of 10% to 30% of the code. Only code gets compressed, no pictures or other hard-weighted project resources. In parallel with code downsize, performance can be increased by 5%.

There are more enhancements that allow doing code size minimization but performance maximization.

Runtime Optimizations:

Calling Convention will remove all unnecessary calls for an object’s usage, that allows the compiler to shorten the code size and improve performance.

The new String type uses 16 bytes (on 64 bytes platform) instead of 24 in the previous Swift version. In the case where the string is not large, it is put into 15 bytes directly, while improving performance and without memory usage.

In new Swift version, the majority of updates were implemented with help of the community. 10 of 24 improvement proposals that were included in release came from the third-party developers.

Swift 4.2 updates from community