What’s New in Swift 5? Feature upgrade on iOS App Development

By May 2, 2019 May 17th, 2019 Swift5

Released in March 2019, Swift 5 is the latest upgrade for the predominant language for Apple. It is also a source-code compatible with Swift 4, Swift 4.1 and Swift 4.2.

To assist the earlier releases of Swift to move to Swift 5, Apple’s Xcode 10.2 incorporates a code migrator which automatically handles most of the needed source code changes. There is also a migration guide to assist developers through the changes in the code, especially the ones that need direct attention.

Swift 5 runtime has become a part of the OS in Apple’s latest platform release.

SDK:

The source-code compatibility changes for the SDK between 4.2 and 5 are minimal and are necessary to improve the accuracy of the APIs.

ABI Stability:

ABI (Application Binary Interfaces) is the binary equivalent of API (Application Programming Interface) and is compatible with every version of Swift. If the code written becomes compatible with all the new versions of the language, the developer doesn’t have to update all the external dependencies of the project while migrating.

Why does ABI Stability matter?

  • Bundle size will be reduced
  • Language changes smaller/Less frequent
  • Less migration
  • Developers can create Pre-compiled Frameworks in Swift (at present frameworks are compiled when compiling the app) for there is no need to embed Swift.

Drawbacks?

  • Limits the changes to the Public Interfaces and Symbols
  • Restricts the ways Swift can grow and evolve.

Actor:

  •  An actor is a new ‘type‘ in Swift, like class, struct, and protocol.
  • An actor enables the programmer to define internal variables and functions to manage data and perform operations.
  • Actors can’t return values, throw errors, or have in/out parameters.
  • Actor shuts down when their reference count reaches 0 or when the last queued message is completed.

Example:

actorTableModel {
letmainActor : TheMainActor
vartheList : [String] = [] {
didSet {
mainActor.updateTableView(theList)
}
}
init(mainActor: TheMainActor) {
self.mainActor = mainActor
}
actorfunc add(entry: String) {
theList.append(entry)
}
}

Escaping Raw Strings:

Swift 4.2 employed escape sequences to represent backslashes and quotations in strings.

Meanwhile, Swift 5 adds raw strings. Add # at the beginning and end of the string so to use backslashes and quotations without any issues.

To use raw strings, place one or more `#` before your strings which would look like
let keypaths = #”Swift keypaths such as \Person.the name holds uninvoked references to properties.”#

New Unicode Scalar Properties – Swift 5 adds properties to Unicode scalars, which simplify text processing

Integer Multiples  -isMultiple(of:)

It allows us to check whether one number is a multiple of another in a clear way than using the division remainder operation `%`.

Example:

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

Result :

The result is a new ‘type’ in Swift which gives a simple and efficient way to handle errors in complex code as in asynchronous APIs. Swift’s Result type is executed as an enum that has only two cases: success and failure

Example:

URLSession.shared.dataTask(with: url) { (result: Result<(response: URLResponse, data: Data), Error>) in // Type added for illustration purposes.
switch result {
case let .success(success):
handleResponse(success.response, data: success.data)
case let .error(error):
handleError(error)
}
}

Leave a Reply