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)

Swift features

When Apple first introduced Swift, it said that Swift is Objective-C without the C. This really only tells us half of the story. Objective-C is a superset of C and provides object-oriented capabilities and a dynamic runtime to the C language. This meant that with Objective-C, Apple needed to maintain compatibility with C, which limited the enhancements it could make to the Objective-C language. As an example, Apple could not change how the switch statement functioned and has still maintained compatibility with the C language.

Since Swift does not need to maintain the same C compatibility as Objective-C, Apple was free to add any feature/enhancement to the language. This allowed Apple to include the best features from many of today's most popular and modern languages, such as Objective- C, Python, Java, Ruby, C#, and Haskell.

The following chart shows a list of some of the most exciting enhancements that Swift offers compared to the Objective-C language:

Swift feature

Description

Type inference

Swift can automatically deduce the type of a variable or constant, based on the initial value.

Generics

Generics allow us to write code only once to perform identical tasks for different types of object.

Collection mutability

Swift does not have separate objects for mutable or non- mutable containers. Instead, you define mutability by defining the container as a constant or variable.

Closure syntax

Closures are self-contained blocks of functionality that can be passed around and used in our code.

Optionals

Optionals define a variable that might not have a value.

Switch statement

The Switch statement has been drastically improved. This is one of my favorite improvements.

Tuples

Functions can have multiple return types using tuples.

Operator overloading

Classes can provide their own implementation of existing operators.

Enumerations with associated values

In Swift, we can do a lot more than just define a group of related values with enumerations.

Protocols and protocol-oriented Design

Apple introduced the protocol-oriented programming paradigm with Swift version 2. This is a new way of not only writing applications but also changing how we think about programming.

Before we begin our journey into the wonderful world of Swift development, let's take a detour and visit a place that I have loved ever since I was a kid: the playground.