Features Of Swift 5

xpertlab-6g
Next Generation Of Network 6G
20th April 2019
xpertlab-hacking
Hacking
2nd May 2019

Great news! Swift 5 is finally available in Xcode 10.2! This release brings ABI stability and improves the language with some long-awaited features.

The Swift runtime is now included in current and future versions of Apple’s platform operating systems: macOS, iOS, tvOS and watchOS.

Swift 5 also introduces new capabilities that are building blocks for future versions, including a reimplementation of String, enforcement of exclusive access to memory during runtime, new data types, and support for dynamically callable types.

1. ABI Stability :

  • At runtime Swift binaries interact with other libraries and components through an ABI (Application Binary Interfaces). It defines low level details like how to call a function, how data is represented in memory and where metadata is and how to access it.
  • Currently Swift is not ABI stable, so each binary (App), bundles its own version of the Swift Dynamic Library. Swift doesn’t live on the iOS Operating System, it lives within each App.
  • That means any app (lets App1) is using Swift 3.0, so it bundles Swift 3.0 Dynamic Library (containing the 3.0 ABI) inside. But the any other app (let App2) is using Swift 3.2, so it bundles Swift 3.2 and it’s 3.2 ABI.
  • If Swift becomes ABI Stable, Swift will live within the iOS Operating System and it’s ABI will be compatible with every version of Swift. i.e App1 is using Swift 5.0, but App2 is using Swift 5.3, and their both consuming Swift ABI embedded in the Operating System.
  • If Swift becomes ABI Stable, Swift will live within the iOS Operating System and it’s ABI will be compatible with every version of Swift. i.e App1 is using Swift 5.0, but App2 is using Swift 5.3, and their both consuming Swift ABI embedded in the Operating System.
  • Swift 5.0 is not binary compatible with earlier Swift releases. Binary compatibility allows Swift code compiled by different Swift compilers to link together and interoperate at a runtime level. However, future Swift releases will be binary compatible with Swift 5.

Why ABI Stability

  • Bundle size will reduced
  • Language changes smaller / Less frequent
  • Less Migration
  • Developers can create Pre-compiled Frameworks in Swift (currently frameworks are compiled when compiling your app) because no need to embed Swift

2. Async/Await Pattern :

  • Well known solution used in other popular programming languages – C#PythonJavascript with great success.
  • Async keyword used similar to the existing throws keyword
  • Declare a function as async to indicate function is a Coroutine.
  • Similar to the existing try keyword.
  • Indicates that non-local control flow can happen at that point.
  • Asynchronous APIs are difficult to work. Error Handling, Callbacks, when performing multiple operations creates complicated control flows.
  • Made worse with Swift’s guard let syntax throughout callback closures.
  • Hard to know what Queue/Thread you’re on.

3. Actor :

  • Actors represent real world concepts like “a document”, “a device”, “a network request”.
  • An actor is a combination of a Dispatch Queue, the data that queue protects and messages that can be run on the queue.
  • It would be a new ‘type‘ in Swift, like class, struct or protocol
  • An actor allows programmer to define internal variables and functions to
    manage that data and perform operations on it.
  • Actors can’t return values, throw errors or have in out parameters
  • We can communicate with Actors asynchronously, and actors guarantee that the data they protect is only touch by the code running on that queue.
  • UIKit and AppKit would model something like the “Main Thread” using a MainActor
  • Programmers could define extensions to the Main Actor in order to run their code on the main thread.
  • Actors are shutdown when their reference count reaches 0 and the last queued message is completed.

Some More Features :

  • String – Language level support for Regular Expressions. Performance Improvements to the internal implementation of String.
  • Standard Library Improvements
  • Foundation Improvements
  • Syntactic Additions