Book Image

Mastering Swift 5 - Fifth Edition

By : Jon Hoffman
Book Image

Mastering Swift 5 - Fifth Edition

By: Jon Hoffman

Overview of this book

Over the years, the Mastering Swift book has established itself amongst developers as a popular choice as an in-depth and practical guide to the Swift programming language. The latest edition is fully updated and revised to cover the new version: Swift 5. Inside this book, you'll find the key features of Swift 5 easily explained with complete sets of examples. From the basics of the language to popular features such as concurrency, generics, and memory management, this definitive guide will help you develop your expertise and mastery of the Swift language. Mastering Swift 5, Fifth Edition will give you an in-depth knowledge of some of the most sophisticated elements in Swift development, including protocol extensions, error handling, and closures. It will guide you on how to use and apply them in your own projects. Later, you'll see how to leverage the power of protocol-oriented programming to write flexible and easier-to-manage code. You will also see how to add the copy-on-write feature to your custom value types and how to avoid memory management issues caused by strong reference cycles.
Table of Contents (20 chapters)

What is Swift?

Swift is a programming language that was introduced by Apple at the World Wide Developers Conference (WWDC) in 2014. Swift was arguably the most significant announcement at WWDC 2014 and very few people, including Apple insiders, were aware of the project's existence prior to it being announced.

It was amazing, even by Apple's standards, that they could keep Swift a secret for as long as they did and that no one suspected they were going to announce a new development language. At WWDC 2015, Apple made another big splash when they announced Swift 2. Swift 2 was a major enhancement to the Swift language. During that conference, Chris Lattner said that a lot of the enhancements were based on direct feedback that Apple received from the development community. It was also announced that Swift would become an open-source project. In my opinion, this was the most exciting announcement of WWDC 2015.

In December 2015, Apple officially released Swift as open-source with the https://swift.org/ site, which is dedicated to the open-source Swift community. The Swift repository is located on Apple's GitHub page (http://github.com/apple). The Swift evolution repository (https://github.com/apple/swift-evolution) tracks the evolution of Swift by documenting the proposed changes. A list of which proposals were accepted and which were rejected can be found in the evolution repository. In addition to these resources, Apple has moved away from using mailing lists as the primary form of communication with the Swift community, and has set up Swift forums (https://forums.swift.org).

Swift 3, which was released in 2016, was a major enhancement to the Swift language that was not source-compatible with previous releases of the Swift language. It contained fundamental changes to the language itself and to the Swift standard library. One of the main goals of Swift 3 was to be source-compatible across all platforms, so the code that was written for one platform would be compatible with all other platforms. This means that the code we develop for macOS should work on Linux.

In September 2017, Swift 4 was released. One of the primary goals of the Swift 4 compiler is to be source-compatible with Swift 3. This will allow us to compile both Swift 3 and Swift 4 projects with the Swift 4 compiler. Apple has established a community-owned source-compatibility test suite that will be used to regression test changes to the compiler.

Projects that are added to the test suite will be periodically built against the latest development version of Swift to help understand the impact of the changes being made to Swift. You can find the Swift source compatibility page here: https://swift.org/source-compatibility/.

One of the original goals of Swift 4 was to stabilize the Swift Application Binary Interface (ABI). The main benefit of a stable ABI is to allow us to distribute frameworks in a binary format across multiple versions of Swift. If a stable ABI is in place, we would be able to build a framework with the Swift 4 compiler and have it work with applications that were written in future versions of Swift. This feature ended up being deferred to Swift 5.

Now that Apple has released Swift 5, the ABI has been declared stable for all Apple platforms. You can read Swift's ABI Stability Manifesto here: https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md. As development for Swift on other platforms, such as Linux, matures, the Swift Core team has said that they will evaluate stabilizing the ABI for those platforms as well. A stable ABI means that a library that is compiled for one version of Swift, let's say Swift 5, will theoretically work with future versions of Swift without having to be recompiled.

The last version of the Mastering Swift series was released when Swift 4.0 was released. Since that time, there have been numerous enhancements to the Swift language with two major versions, Swift 4.2 and Swift 5, being released. Throughout this book, we will see how to use a number of these enhancements.

The development of Swift was started in 2010 by Chris Lattner. He implemented much of the basic language structure when only a few people were aware of its existence. It wasn't until late 2011 that other developers began to contribute to Swift. In July 2013, it became a major focus of the Apple Developer Tools group.

Chris started working at Apple in the summer of 2005. He held several positions within the Developer Tools group and was the director and architect of that group when he left Apple in 2017. On his home page (http://www.nondot.org/sabre/), he notes that Xcode's playground (we'll talk more about playgrounds a little later in this chapter) became a personal passion of his because it makes programming more interactive and approachable. If you are using Swift on the Apple platform, you will be using playgrounds a lot as a test and experimentation platform. You can also use Swift Playgrounds on the iPad.

There are a lot of similarities between Swift and Objective-C. Swift adopts the readability of Objective-C's named parameters and dynamic object model. When we refer to Swift as having a dynamic object model, we are referring to the ability for types to change at runtime. This includes adding new (custom) types and changing/extending the existing types.

While there are a lot of similarities between Swift and Objective-C, there are significant differences between them as well. Swift's syntax and formatting are a lot closer to Python than Objective-C, but Apple did keep the curly brackets. I know Python people would disagree with me, and that is all right because we all have different opinions, but I like the curly brackets. Swift actually requires the curly brackets for control statements, such as if and while, which eliminates bugs, such as the goto fail in Apple's SSL library.