Mallow's Blog

What’s new in Swift 4.2 – A brief introduction

Apple has released Swift 4.2 which seems to be a preliminary step for the launch of Swift 5 next year. We will now see some of the improvement in Swift 4.2 which may come in handy in our projects.

Random number:

Before Swift 4.2:

let randomNumber = arc4random_uniform(50)

The above code generates a random number from 1 to 50 of UInt32 type.

In Swift 4.2:

let randomNumber = Int.random(in: 1..<50)

Here the above code generates number from 1 to 50 of Int type. The above method applies for similar data types like Float, Double, CGFloat, etc. In case if you were to generate a random bool, you can do easily with Bool.random(). One more addition to the above is we can also generate a random from an array like

Let cars = [“Ford”, “Toyota”, “Volkswagen”, “Benz”]

print(cars.randomElement()) // Will print any one from above cars array.



Shuffling property might come in handy in places where you generate a permutation randomly somewhere like card games.

var cards = [“Jack”, “king”, “Queen”]

cards.shuffle() // Will reorder the array elements in place

or let cards = cards.shuffled() // Will assign the reordered array back to cards variable


Improvements in Sequence naming:

Before Swift 4.2:

let array = [“apple”, “mango”, “orange”]

if let appleFruit = array.index(where: { $0  == “apple”) }), {
     print(“Found apple“)
}  else {
     print(“No apples found”)

The above code takes the index of first element in array which is equal to “apple”

In swift 4.2

if let firstFruit = array.firstIndex(where: { $0  == “apple”) }), {
     print(“Found apple“)  
} else {
     print(“No apples found”)

The index(where:) becomes firstIndex(where:) for proper naming conventions and avoiding confusions.

Enum’s new all cases:

Often we come across a scenario where need a collection of all the available enum types.

Before swift 4.1:

enum Cars: String {

case Ford = “Ford”

case Benz = “Benz”

case Audi = “Audi”

static let allValues = [Ford, Benz, Audi]


Say for example if we need to print the list of all the cars, then we have to add an additional variable called allCars which is an array of all the available types.

In Swift 4.2:

We have an new allCases property by default which lists all the available enums, so that we don’t have to manually add a property for it. However, we must add CaseIterable to the declaration for the allCases property to be available.

We can call the property like

enum Cars: CaseIterable {

case Ford = “Ford”

case Benz = “Benz”

case Audi = “Audi”


for car in Cars.allCases {

print(car) // Will print “Ford”, “Benz”, “Audi”



#warning and #compiler directives

Often we use something called //TODO:- which would remind developer to do some task at a later point of time  or a //FIXME: – which is an error or issue that the developer has to fix it. But the problem with above is that, there is a high chance of developer neglecting the above changes. To trigger a warning for every “TODO” or “FIXME” tags, you might have to add a build script in XCode which is something like:


find "${SRCROOT}" ( -name ".h" -or -name ".m" -or -name ".swift" -type f) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).\$" | perl -p -e "s/($TAGS)/ warning: \$1/"

From Swift 4.2:

They have added new compiler directives that help us mark issues in our code.

#warning: This is useful reminder about some tasks that the developer has to look on later.

func example() {

/* — */
#warning("This method needs to be improved ")


#error: This is useful when you cannot proceed without FIXING some issues. Like  providing an api key when you ship a library

func credentials() {

let username = “”
ler password = “”

if username.isEmpty || password.isEmpty {
#error(“User name and password is mandatory”)

/* — */




Swift 4.2 improves upon many Swift 4.1 features which makes developer’s life easier and also paves the way for Swift 5. It was great working with Swift 4.2 and looking forward from Swift 5 which will be released early next year.

Rajtharan G,
iOS Development Team,
Mallow Technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *