Swift 4.2 is just released by Apple, which enables new possibilities to write better code in Swift. This post will dive into the changes!

Iterate trough an enum

In the past, when you wanted to loop trough all values of an enum, you had to define a static array with all the enum options in the array. Swift 4.2 allowing you to use the CaseIterable protocol. If your array is conform that protocol, you are able to use the function AllCases to get an array of all the enum options.

enum Storyboards: CaseIterable {
    case main, onboarding, shop

let allStoryboards = Storyboards.AllCases

Random value from array

It was always hard to receive a random value from an array, but that time is over! From now on, you can call the random() function which returns a random value from an array!

let randomInteger = Int.random(in: 1 ... 3)
let randomFloat = Float.random(in: 0 ... 10)

let randomBool = Bool.random()

Shuffle array values

There is now an easy way to shuffle values of an array. This could be used with strings or ints, or any other array you have! Using the function shuffled() the array will be reversed.

var numbers = [1, 2, 3, 4, 5]
var names = ["Jeroen", "Cees", "Lee", "James"]



Toggle is introduced for booleans. The function will do the opposite of the current value of a boolean. Using the toggle function, code is more readable.

// Old situation
var isActive = true
isActive = !isActive

// New situation
var isActive = true


In the past, SwiftLint was needed to highlight TODO comment lines in code, to be sure you won’t forget to implement a functionality before the app will go live. With Swift 4.2, the #warning function is introduced. When using the #warning function, the comment will be listed in the issue navigation in Xcode.

#warning("TODO: Will be implemented next sprint")