Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Exclusivity enforcement is now complete in Swift 5

Save for later
  • 2 min read
  • 06 Feb 2019

article-image

Yesterday Apple talked about exclusivity enforcement in Swift 5, in a post. No this is not some exclusive feature or patenting of some sort. This idea is on how variables in a Swift program access memory. Swift is the programming language used for developing Apple apps.

What is exclusivity enforcement?


The Swift 5 release allows runtime checks on “Exclusive Access to Memory”. This further adds to Swift showing that it is a ‘safe language’. For memory safety to take place, Swift needs exclusive access to a variable and modify it. This means that the variable can be accessed only with the same name when it is being modified as particular arguments. A programmer’s intention in case of exclusivity violations is often ambiguous in Swift. So, to protect against it and to allow the safety features, exclusivity enforcement was introduced in Swift 4.

In Swift 4, both compile-time and run-time enforcement was available, the latter being available only in debug builds. Some of the holes in exclusivity enforcement are patched in Swift 5 by changing the language model. So runtime exclusivity enforcement is enabled by default in Release builds.

This can impact Swift projects in two ways:

  1. Violation of Swift exclusivity rules causing a runtime trap
  2. Overhead due to memory access checks can degrade performance slightly

Why exclusivity enforcement?


This enforcement is done mainly to enforce memory safety in Swift.

  1. It eliminates dangerous interactions in Swift programs which involves mutable states
  2. Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at €18.99/month. Cancel anytime
  3. Enforcement gets rid of unspecified behavior rules from Swift
  4. It is mandatory to maintain ABI stability
  5. In addition to protecting memory safety, this enforcement helps in optimizing performance
  6. The exclusivity rules give programmers the control to move only types


Even though the memory problem is a rare occurrence, addressing it early on improves Swift a bit. A comment on Hacker news says: “The benefit being that you only have to deal with this issue rarely, rather than all the time with manual memory management.

Apple is patenting Swift features like optional chaining

Swift 5 for Xcode 10.2 beta is here with stable ABI

Swift is now available on Fedora 28