Migrating to Swift 5

Updating Swift code is easier than ever and the migration assistant takes care of most of the work

As Swift matures, it has become increasingly stable. Thankfully, the task of migrating between major releases of the language has gotten easier. Anyone who has worked with Swift since the beginning will shudder when remembering the migration from Swift 2 to 3.

Swift 5 is notable becase it marks the first release with ABI stability. This will lead to smaller application sizes – the Swift libraries will not need to be bundled in every application.

Moving a codebase to Swift 5 is relatively painless and I have only encountered a few issues in my codebase.

In Swift 5, the compiler will raise a warning if you are switching over an enum from AppKit without adding an @unknown case. For example, switching over the AppKit enum NSTableView.RowActionEdge now requires something like this:

switch edge {
  case .leading:

  case .trailing:

  @unknown default:

The Swift standard library has now added a lastIndex(of:) method to find the last occurance of an element in a collection, thus index(of:) has been renamed to firstIndex(of:).

Finally, results of calls to try? will still return an optional, but they will no longer be nested optionals. I welcome this change, this simplifies code that was confusing and means no longer having to use a double if-let to get the required value.

Found this useful? Take a look at more articles about macOS development.