What’s new in Swift 4 from Swift 3?

Abhishek Kumar
Sep 2, 2018 · 3 min read

Let’s get started one by one major features introduced/modified/updated in Swift 4.

One-sided Ranges:

This proposal introduces the concept of a “one-sided” range, created via prefix/postfix versions of the existing range operators.

It is common, given an index into a collection, to want a slice up to or from that index versus the start/end.

let s = "Hello, World!"
let i = s.index(of: ",")!
let greeting = s[s.startIndex..<i]

When performing lots of slicing like this, the verbosity of repeating s.startIndex is tiresome to write and harmful to readability.

Solution in Swift 4:

Introduce a one-sided range syntax, where the “missing” side is inferred to be the start/end:

// half-open right-handed range
let greeting = s[..<i]
// closed right-handed range
let withComma = s[...i]
// left-handed range (no need for half-open variant)
let location = s[i...]

To enumerate dictionary, we needed to iterate from start to get the tupple (key, value).

for (index, value) in array.enumerated() {

print(“\(index + 1): \(value)”)

}

for (index, value) in zip(1…, array) {

print(“\(index): \(value)”)

}

You use the zip(_: _:) function inside a for in loop in order to combine the one sided range of indexes with the array’s values – the first index is 1 now.

swap vs swapAt :

The swap(_:_:) mutating method in Swift 3 takes two elements of a certain array and swaps them on the spot:

var numbers = [1, 50, 21, 80, 42, 10]

swap(&numbers[0], &numbers[1])

This solution has one major drawback: the swapped elements are passed to the function as inout parameters so that it can access them directly. Swift 4 takes a totally different approach by replacing the method with a swapAt(_:_:) one which takes the two elements corresponding indices and swaps them just as before:

numbers.swapAt(0, 1)

This will swap elements of array at indices o and 1.

Private vs Fileprivate in Extensions:

In Swift 3 extensions doesnot have access to private variables. So, developer need to declare fileprivate in order for those variables to be able to access from extensions.

Improved Emoji Strings:

Swift 3 doesn’t determine the number of characters in emoji strings correctly:

let emojiString = “👨‍👩‍👧‍👦”
let characterCountSwift3 = emojiString.characters.count

//Swift 3 will return 4.

Swift 4 fixes this like below.

let characterCountSwift4 = emojiString.count // returns 1

Multiple Line Strings :

Swift 4 introduces “”” (triple quotes).

let multiLineStringSwift4 = “””

You can display strings

on multiple lines by placing a

\””” delimiter on a separate line

both right at the beginning

and exactly at the very

end of it

and don’t have to “escape” double quotes

“” in Swift 4.

“””

Strings as Collections :

Strings are collections in Swift 4, so you can now treat them like arrays or sequences and simplify certain tasks. For example, this is how you would filter a given string in Swift 3:

New Substring type :

Swift 4 brings new Substring type which represents a subsequence of String.

Long-term storage of `Substring` instances is discouraged. A substring holds a reference to the entire storage of a larger string, not just to the portion it presents, even after the original string’s lifetime ends. Long-term storage of a substring may therefore prolong the lifetime of elements that are no longer otherwise accessible, which can appear to be memory leakage.
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade