Tuesday, January 26, 2021
Techiexpert.com
No Result
View All Result
  • Login
  • Register
  • Home
  • Tech news
  • Startups
  • AI
  • IOT
  • Big Data
  • Cloud
  • Data Analytics
  • ML
  • Blogging
Techiexpert.com
No Result
View All Result

5 Features of The New Apple’s Swift 5 Language

Harry Miller by Harry Miller
July 12, 2019
in Mobile Tech
Reading Time: 5min read
A A
0
5 Features of The New Apple's Swift 5 Language 1
16
SHARES
185
VIEWS
Share on FacebookShare on Twitter

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.

ADVERTISEMENT

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.

Tags: AppleiOS
Share8Tweet3Share1Pin1
Harry Miller

Harry Miller

Related Posts

Google announces ‘Journalism AI’ project in partnership with Polis
Mobile Tech

Google Pay Features Available in latest updates

December 19, 2020
5 Guiding Principles Of Mobile-First Web Design
Mobile Tech

5 Guiding Principles Of Mobile-First Web Design

December 17, 2020
5 Best Weather Apps You Can Use in 2020
Mobile Tech

5 Best Weather Apps You Can Use in 2020

November 27, 2020
How to build dApps that reward network participants?
Mobile Tech

10 Reasons Why Your Business Needs a Mobile App

October 13, 2020
Top Tips On How To Build Your Own Chat App
Mobile Tech

Top Tips On How To Build Your Own Chat App

September 24, 2020
Reasons To Opt React Native
Mobile Tech

Hard Core Reasons To Opt React Native For PWA Development

September 24, 2020

Leave a Reply Cancel reply

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

I agree to the Terms & Conditions and Privacy Policy.

Latest Stories

Why Charter Networks Are Investing Heavily in Big Data
Tech news

Best Practices Of Financial Investing: Here Is What Experts Are Saying

by Sony T
January 26, 2021
Paid VS Organic Online Traffic: Why you should use them in tandem? 2
Marketing Trends

Paid VS Organic Online Traffic: Why you should use them in tandem?

by Sony T
January 25, 2021
Alibaba Cloud Computing Now Ranked Third-Largest Infrastucture as a Service Provider
Cloud Computing

Alibaba Cloud Computing Now Ranked Third-Largest Iaas Provider

by Srikanth
January 25, 2021
How a SERP Checker Can Improve Your Search Rankings
Marketing Trends

How a SERP Checker Can Improve Your Search Rankings

by Srikanth
January 23, 2021
Meet India’s Atmanirbhar Microprocessor chip ‘Moushik’, meant for IoT devices
Internet Of Things

Meet India’s Atmanirbhar Microprocessor chip ‘Moushik’, meant for IoT devices

by Srikanth
January 22, 2021
Load More
Techiexpert.com

© 2020 All Rights Reserved

  • Terms of use
  • Privacy Policy
  • About Us
  • Contact us
  • Write For Us
  • Cookie Policy

  • Login
  • Sign Up
No Result
View All Result
  • Home
  • Tech news
  • Startups
  • AI
  • IOT
  • Big Data
  • Cloud
  • Data Analytics
  • ML
  • Blogging

© 2020 All Rights Reserved

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.