Swift 4.2 Release: What's new in this release

Jan 23 2019Written by Manektech Team

Swift 4.2 is out now and shipped with Xcode 10. This release updates important Swift 4.1 features and a lot of code improvements which we will discuss in this blog.

Swift 4.2 has faster builds. Language features to improve efficiency. SDK improvements for Swift. Before we start discussing version 4.2 further please take a look on swift version history.

Here you will find the improvement in language a bit detail so that you can have a better idea about the changes. Some of these changes are easy to implement. Let’s check it out.

Run-time Optimisation :

In an earlier version of Swift its every time retain and release when we use some object, but in Swift 4.2 Apple has removed that extra retain (Now Guaranteed (+0 retain) ?) and release which you can check below.

class X { … }
func caller() {
// ‘x’ created with +1 reference count
let x = X()
}
foo(x)
func foo(x: X) {
let y = x.value
// Calling Convention: “Owned” (+1 retain)
class X { ... }
func caller() {
// ‘x’ created with +1 reference count
let x = X()
foo(x)
}
func foo(x: X) {
let y = x.value
...
// release x
}
Sequence method:

In earlier version of swift if you want to check that whether all the elements are event or not you need
to call the function display below.

let numbers = [8, 12, 54, 64, 18, 32]

let evenNumbers = !numbers.contains { $0 % 2 == 1 }


In Swift 4.2 apple has added this missing method to Sequence

let numbers = [8, 12, 54, 64, 18, 32]

let evenNumbers = numbers.allSatisfy { $0 % 2 == 0 }

Dynamic Member Lookup:
In an earlier version, we are used to doing code like below.
class Hotel {
let name: String
let rooms: Int
private let details: [String: String]

init(name: String, rooms: Int, details: [String: String]) {
self.name = name
self.rooms = rooms
self.details = details
}
subscript(key: String) -> String {
switch key {
case "info":
return "\(name) has \(rooms) total rooms."
default:
return details[key] ?? ""
}
}
}

let details = ["type": "Delux Suit", "Rate": "18000"]
let hotel = Hotel(name: "Courtyard Marriot", rooms: 134, details: details)
hotel["info"] // "Courtyard Marriot has 134 total rooms."
hotel["type"] // "Delux Suit"

In this case, subscript returns message based on hotel’s name and rooms.
Swift 4.2 uses dynamic member lookup to provide dot syntax for subscripts instead of this.
First, you need to mark Hotel as @dynamicMemberLookup to enable dot syntax for its custom subscripts.
Then you call the previously implemented subscript using dot syntax like mentioned below.


@dynamicMemberLookup
class Hotel {
let name: String
let rooms: Int
private let details: [String: String]
init(name: String, rooms: Int, details: [String: String]) {
self.name = name
self.rooms = rooms
self.details = details

}
subscript(dynamicMember key: String) -> String {
switch key {
case "info":
return "\(name) has \(rooms) total rooms."
default:
return details[key] ?? ""
}
}
}

let details = ["type": "Delux Suit", "Rate": "18000"]
let hotel = Hotel(name: "Courtyard Marriot", rooms: 134, details: details)
print(hotel.info) // "Courtyard Marriot has 134 total rooms."
print(hotel.type) // "Delux Suit"

The compiler measures the subscript call ever-changing at run-time, which allows you to write type-safe code.

Generate Random number, element and order:

To generate random numbers is quiet easier now.

Like the earlier version, we need to use some C functions to create random values.

Swift 4.1

let countries = ["India", "China", "Japan", "Singapore"]

let index = Int(arc4random_uniform(UInt32(countries.count)))

let country = countries[index]

print(country) //Will print random country

Swift 4.2

if let country = countries.randomElement() {
print(country)
} else {
print("Empty Country List")
}

In 4.1 we didn’t have any collection shuffling algorithms and we had to use some code like below.

Swift 4.1
var animals = ["Lion", "Tiger", "Jaguar", "Elephant"]
animals.sort { _, _ in arc4random_uniform(2) == 0 }
print(animals) //print shuffled animal list

Swift 4.2
animals.shuffle()
print(animals)

Collection of Enum Cases

If we need to use a separate function to get all enum cases while in Swift 4.2 we can use CaseIterable to get all cases.
earlier if we add some new case enum, you also need to check that new case needs to be added in all cases array, but by using CaseIterable now no need to take care of that thing.

//Derived Collection of Enum Cases
enum Animal {
case dog

case cat

case monkey

case kangaroo

case lion
static var allCases: [Animal] = [.dog, .cat, .monkey, .kangaroo]
}

for animal in Animal.allCases {
//This code will never print “lion”
print(animal)
}


//Derived Collection of Enum Cases with using CaseIterable
enum Animal: CaseIterable {
case dog

case cat

case monkey

case kangaroo

case lion

}

for animal in Animal.allCases {

//This code will print all cases

print(animal)
}

Other stuff which is added in this new version

Replacing // TODO: with the new #warning
In objective C we used to use #warning to mark some warning and when swift introduced we have
accepted //TODO: instead of this #warning tag, but now in Swift 4.2 we again have that warning tag.
We also have #error tag now to display errors.

We can now have toggle function to change the value of Boolean.

Contact us

Here to Help ambitious people TRANSFORM the Digital Solutions!