Swift

Swift 4.2 - What's New

In this post, I would like to share what’s new in Swift 4.2. All the code displayed in this post is available at my GitHub repo my-learnings/Swift4.2.

Random Number Generation

  • Swift 4.2 added random number generator API to standard library. You can use it on Int, Double, Float, CGFloat and Bool.
  • It also provides a convenient API randomElement which returns a random element from a sequence
  • It also provides the APIs shuffle and shuffled to shuffle a sequence
1
2
let randomInt = Int.random(in: 0..<10)
let randomElement = ["one", "two", "three", "four"].randomElement()

Dynamic Member Lookup

Swift 4.2 introduces a dot syntax to access custom subscripts; this is much cleaner than the earlier square bracket calls. The compiler evaluates the subscript call dynamically at runtime, but provides a cleaner syntax.