Book Image

Mastering Swift 3

Book Image

Mastering Swift 3

Overview of this book

Swift is the definitive language of Apple development today. It’s a vital part of any iOS and OS X developer’s skillset, helping them to build the most impressive and popular apps on the App Store—the sort of apps that are essential to iPhone and iPad users every day. With version 3.0, the Swift team have added new features to improve the development experience—making it easier to get the results you want and customers expect. Inside, you’ll find the key features of Swift 3.0 and quickly learn how to use the newest updates to your development advantage. From Objective-C interoperability to ARC, to closures and concurrency, this advanced Swift guide will develop your expertise and make you more fluent in this vital programming language. We give you in-depth knowledge of some of the most sophisticated elements of Swift development including protocol extensions, error-handling, design patterns, and concurrency, and guide you on how to use and apply them in your own projects. You'll see how even the most challenging design patterns and programming techniques can be used to write cleaner code and to build more performant iOS and OS X applications. By the end of this book, you’ll have a handle on effective design patterns and techniques, which means you’ll soon be writing better iOS and OS X applications with a new level of sophistication and control.
Table of Contents (23 chapters)
Mastering Swift 3
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Free Chapter
1
Taking the First Steps with Swift
2
Learning About Variables, Constants, Strings, and Operators

What is Swift?


Swift is Apple's new programming language, introduced at the Worldwide Developers Conference (WWDC) in 2014 alongside the integrated development environment Xcode 6 and iOS 8. 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 were able to 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 Xcode 7 and 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 the direct feedback that Apple received from the developer community.

On December of 2015, Apple officially released Swift as an open source project, with the swift.org site 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 for Swift. You can also find a list of which proposals were accepted and which were rejected in the evolution repository. If you are interested in understanding where Swift is heading, you should check out this repository. It is interesting to note that Swift 3 contains several enhancements that were proposed by the community.

Swift 3 is a major enhancement to the Swift language that is NOT source compatible with previous releases of the Swift language. It contains fundamental changes to the language itself and to the Swift standard library. One of the main goals of Swift 3 is to be source compatible across all platforms, so the code that we write for the one platform will be compatible with all other platforms. This means that the code we develop for Mac OS will work on Linux, although certain frameworks, such as UIKit, may not be compatible across platforms.

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

Chris Lattner started working at Apple in the summer of 2005. He has held several positions in the Developer Tools group and is currently the director and architect of that group. On his home page (http://www.nondot.org/sabre/), he notes that Xcode's Playgrounds (you can read more on Playgrounds a little later in this chapter) became a personal passion of his because it makes programming more interactive and approachable. We will be using Playgrounds a lot in this book as a test and experimentation platform. Starting with iOS 10, we will be able to use Swift Playgrounds on the iPad.

Note

To me, being able to use Swift Playgrounds on the iPad is very exciting because it will make it easier for people getting started with programming to learn the Swift language. I am really looking forward to showing my daughters how to use Playgrounds on their iPads.

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 of 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 braces. I know Python people would disagree with me, and that is all right because we all have different opinions, but I like the curly braces. Swift actually makes the curly braces required for control statements, such as if and while, which eliminates bugs, such as goto fail in Apple's SSL library.

Swift was also built to be fast. At WWDC 2014, Apple showed a number of benchmarks, which proved that Swift significantly outperformed Objective-C. Swift uses the LLVM compiler, which is included with Xcode 7 to transform Swift code into highly optimized native code that is tuned to get the most out of Apple's modern hardware.

Swift features

When Apple said that Swift is Objective-C without the C, they were really only telling 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 still maintain the C compatibility.

Since Swift does not need to maintain the same C compatibility as Objective-C, Apple was free to add any features/enhancements 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#, Haskell, and many others.

The following chart shows a list of some of the most exciting enhancements that Swift includes:

Swift feature

Description

Type inference

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

Generics

Generics allow us to write code once to perform identical tasks for different types of objects while still maintaining type safety.

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 with features such as pattern matching, guard conditions, and no automatic fall-through. This is one of my favorite improvements.

Multiple return types

Functions can have multiple return types using tuples.

Operator overloading

Classes can provide their own implementation of the existing operators.

Enumerations with Associated values

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

There is one feature that I did not mention in the preceding chart because it is technically not a feature of Swift; it is a feature of Xcode and the compiler. This feature is Mix and match. Mix and match allow us to create applications that contain both Objective-C and Swift files. This allows us to systematically update our current Objective-C applications with Swift classes and also use Objective-C libraries/frameworks in our Swift applications.

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.