Skip to content

Instantly share code, notes, and snippets.

@AdrianBinDC
Created August 5, 2018 00:30
Show Gist options
  • Save AdrianBinDC/132a1e8feca060836ef314e51c82f5af to your computer and use it in GitHub Desktop.
Save AdrianBinDC/132a1e8feca060836ef314e51c82f5af to your computer and use it in GitHub Desktop.
New in Swift 4.2

New in Swift 4.2

Summary from HackingWithSwift.com

CaseIterable

The CaseIterable protocol provides an allCases method that turns all of your cases into an array.

enum Pasta: String, CaseIterable {
    case canneloni = "Cannelloni pasta"
    case fusilli = "Fusilli pasta"
    case linguini = "Linguini pasta"
    case taliatelle = "Taliatelle pasta"
}

Pasta.allCases.forEach{ pastaType in
    print("\(pastaType.rawValue)")
}

Random number generator

Random number generator for range. Cleaner syntax than arc4random_uniform()

let randomNumber = Int.random(in: 1...1000)

allSatisfy

.allSatisfy returns a Bool for conditions specified on an array

let scores = [85, 84, 95, 92]
let passed = scores.allSatisfy{ $0 >= 85 } // returns Bool

removeAll

.removeAll now allows you to specify a filter to remove items from an array

var theBigLebowski = ["The Dude", "Angry Walter", "Maude Lebowski", "Donny Kerabatsos", "The Big Lebowski", "Little Larry Sellers"]
theBigLebowski.removeAll{ $0.contains("Lebowski")}

toggle()

.toggle() allows you to toggle a boolean without having to assign it

var myBool = true
myBool.toggle() // flips to false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment