WWDC 2018 What’s New in Swift Recap

In What’s New in Swift at WWDC 2018, Apple gave a quick overview of what’s new in Swift 4.2 and Swift 5. You can read my notes below, or you can watch the 40-minute video and download the slides from Apple.


Community updates

Swift 4.2:

  • Faster builds
  • Language features to improve efficiency
  • SDK improvements for Swift
  • Converging towards binary compatibility

Swift 5 (Early 2019):

  • binary compatibility with future Swift releases

Source Compatibility:

  • Compiler supports compatibility modes
  • One compiler with modes

Swift 4.2 SDK Changes

  • Updates to important framework APIs
  • Some API changes will land later in Xcode 10 betas

Xcode 10 is the last release to support Swift 3 compatibility mode

Compile time improvements

  • Compilation mode and Optimization mode are now separate
  • Compilation mode: incremental or whole module
  • Stop using Debug with Whole Module Compilation (kills incremental builds)

Runtime Optimizations

  • Small String: now 16 bytes (memory and performance win)

Reduced Code Size

Optimization:

  • Optimize for Size (app size limits for cellular downloads): makes runtime performance about 5% slower
  • Optimize for Speed

New Language Features in Swift 4.2

Swift Evolution website shows proposals and accepted status

Lots of proposals from the community were implemented

Enums

Now you can print every possible value

In Swift 4: define property with allCases: [Gait] = [.walk, .trot, ...]

Swift 4.2: CaseIterable (compiler eliminates boilerplate)

Equatable

  • If Array element is Equatable, Array is Equatable
  • Same for Optional and Dictionary
  • This is true for Hashable, Encodable, and Decodable as well
  • If Array element is Hashable, Array is Hashable (also true for Optional and Dictionary elements)
  • Same goes for Encodable and Decodable on Array, Optional, Dictionary
  • Summary: Array, Optional, and Dictionary are much easier to use

Synthesized Equatable and Hashable

  • Struct with stored properties, properties are equatable
  • Had to implement Equatable and == func
  • Swift 4.1: synthesize implementation of Equatable and Hashable

What about generics?
enum Either<Left, Right>
– Swift can implement Equatable for you now

Hash combining functions are hard

  • Swift 4.2 adds a better API
  • Hashable now has: func hash(into harsher: inout Hasher)

New hashing algorithm

Hash values vary from run to run (so don’t depend on hash values or order)

Random Numbers, Elements, and Order

  • Not good in Swift 4.0 (arc4random, weird mathy stuff)
  • Now: Int.random(in: 0..<10) // or Float.random(in:)

Collections now have:

  • greetings.randomElement()
  • greetings.shuffled()

Checking platform conditions

Old:

#if os(ios) || os(watchOS) || os(tvOS)
    #import UIKit
// ...

New:

#if canImport(UIKit)

New:

#if hasTargetEnvironment(simulator)

Implicitly Unwrapped Optionals

IUO is an attribute of a declaration, not a type of an expression

Now need to be more explicit

Exclusivity Checking

Improved static and runtime checks

Swift 4.2

Lots of other improvements


Have a great day!