5 Features of The New Apple’s Swift 5 Language

5 Features of The New Apple's Swift 5 Language 1

Let’s look at the new features of Swift 5 Language used to develop IOS apps, Coders are the unsung heroes of this century. We rarely give coders the credit they deserve. Without them, we would not be able to enjoy any of the technology we use in our daily lives.

Things like Spectrum internet ultra would not even exist. This is not as much an opinion as it is a fact. Why? Because, every bit of technology, including the device you are reading this blog on, uses the code.

There are more than 700 programming languages in use today. Some are more advanced and some are limited for use with certain devices, like Swift. Swift is a programming language designed by Apple. macOS, iOS, watchOS, all use the Swift programming language.

Earlier this year, Apple released a welcomed update for its programming language, Swift 5.0. This major update included some new features based on user feedback.

What’s New in the Latest Swift 5.0 Upgrade?

This was the most awaited upgrade since the Swift 4.2 version. The 5.0 version also addressed the biggest issue of ABI stability. Application Binary Interface (ABI) is the interface that links two binary program modules. Simply put, It defines how computational routines are accessed in machine code. However, the new version also offers many other features:

  • Dictionary Values with compactMapValues()
  • Integer Multiples Check
  • Flattening Nested Options
  • Raw Strings

#1. Dictionary Values with compactMapValues()

Using SE-0218 You can add a new compactMapValues() method to dictionaries, bringing together the compactMap() functionality from arrays (“transform my values, unwrap the results, then discard anything that’s nil”) with the mapValues() method from dictionaries (“leave my keys intact but transform my values”).

As an example, check this dictionary of people in one particular race. It includes the names and finishing times in seconds. Marked as “DNF” was one who did not finish:

let times = [
"Mark": "38",
"John": "42",
"Curtis": "35"
"Cole": "DNF"
]

Now we can use compactMapValues() to create a new dictionary. In this one, the names and times are shown as an integer, with the one DNF person removed:

let finishers1 = times.compactMapValues { Int($0) }

Alternatively, you can also pass the Int initializer directly to compactMapValues(), like this:

let finishers2 = times.compactMapValues(Int.init)

Without any transformation, you can also use compactMapValues() to unwrap optionals and discard nil values, like this:

let people = [
"Jack": 38,
"Caroline": 8,
"Sasha": 5,
"Amber": nil
]

let knownAges = people.compactMapValues { $0 }

#2. Integer Multiples Check

Using SE-0225 you can add an isMultiple(of:) method to integers. This allows you to check whether one number is a multiple of another in a much clearer way than using the division remainder operation, %.

For example:

let rowNumber = 4
 if rowNumber.isMultiple(of: 2) {
 print("Even")
 } else {
 print("Odd")
 }

Of course, you could write check using if rowNumber % 2 == 0 but that is less clear – having isMultiple(of:) as a method means it can be listed in code completion options in Xcode. This helps with discoverability.

#3. Flattening Nested Optionals

Using SE-0230 you can modify the way try? works. This way nested optionals can be flattened to become regular optionals. This allows it to function similar to optional chaining and conditional typecasts.

Here’s a practical example that demonstrates the change:

struct User {
var id: Int
init?(id: Int) {
if id < 1 {
return nil
}
self.id = id
}
func getMessages() throws -> String {
// complicated code here
return "No messages"
}
}

let user = User(id: 1)

let messages = try? user?.getMessages()

#4. Raw Strings

Using SE-0200 you can now formulate raw strings on Swift. Instead of using quotation marks and backslashes as string terminators or escapes characters, they are construed as the symbols they are. Use cases become much easier with this method. But what gets the most gains is regular expressions.

Try placing one or more # symbols before your strings, to use raw strings. As illustrated below:

let rain = #”The “rain” in “Spain” falls mainly on the Spaniards.”#

The # symbols that are positioned at the beginning and end of the string sequence are very important. They become part of the string delimiter. Swift 5.0 can understand that the quotation symbols around “Spain” and “rain” need to be considered as simple quotation symbols, instead of string terminators.

You can also use backslashes in raw strings, like this:

let keypaths = #”Swift keypaths such as Person.name hold uninvoked references to properties.”#

This denotes backslashes as actual characters, instead of denoting it as escape characters. That makes the string interpolation work in a completely different way:

let answer = 42

let dontpanic = #”The answer to life, the universe, and everything is #(answer).”#

Note how #(answer) is used for string interpolation. This way, a normal (answer) can be denoted simply as string characters. When you are aiming for string interpolation in a raw string set, the additional # need to be included.

Conclusion

These are some of the new features of the Swift 5.0 programing language. You can find out about many more once you start using it. This is one of the best things about upgrades to languages. This excitement of finding the new features is not comparable to much else.

If it could be compared to anything, it would be like finding out this question most probably: Is 100 Mbps Fast? In addition to all the new features, there is even more good news about the Swift programming language. Apple has already announced that there is a Swift 5.1 version rolling out. The new version will have improvements including module stability.

Report

Written by Harry Miller

Leave a Reply

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

Will IIoT Edge Computing emerge out as a Real Trend in 2019? 5

Will IIoT Edge Computing emerge out as a Real Trend in 2019?

BI Vs Data Analytics: Which is important for your small business 6

BI Vs Data Analytics: Which is important for your small business