Select Page

In September, Apple released Swift 4.0, the latest iteration of its Swift programming language promising improved robustness and stability. While 4 is not as revolutionary as previous versions, lacking the massive selection of new features that came with Swift 2 or any major changes to the syntax, there’s still a lot to love here.

Swift Package Manager

One of the largest upgrades in 4 is improvements to the Swift Package Manager. This tool is a way for developers to easily share and distribute code, automating functions such as downloading and code compilation. 4 improves the Package Manager’s API, giving developers the ability to establish new settings and better organize sources. It has also made it easier for multiple packages to be developed simultaneously.

Codable Protocol

Another significant change made in Swift 4 is the codable protocol, which allows for values to be automatically written to and from JSON without any of the extensive code previously required. Instead, any required code is automatically generated by the protocol. Additionally, developers are given the ability to only encode or decode as needed.

The protocol is customizable, and if you’re already using NSCoding, there’s no need to completely convert your code; it can be paired with the codable protocol to whatever extent you want. There’s a lot of leeway for developers to add in exceptions based on whatever behavior that they need.

Better Strings

Previously, writing out multi-line strings in Swift required either a bit of awkward code or including everything on one line, which hurt readability. In Swift 4, this problem is rectified with a simple fix: all that’s necessary is adding three quotation marks before and after the string in question. Note that all lines must be indented at least as much as the closing quotation marks, and that using this new syntax on a single line will not work.

This new string implementation also simplifies the process for adding and managing substrings. Python had previously done something similar, but with a few differences, so be sure to check your code to ensure that you’ve correctly written these new strings.

Compatibility

One of the main draws of Swift 4 is its backwards compatibility. Apple has stated that the number of source changes are “quite modest” when it comes to adapting from older versions of Swift, and compatibility modes enable developers to migrate at whatever pace they need.