Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Swift 3

You're reading from   Mastering Swift 3 Build incredible apps for iOS and OS X

Arrow left icon
Product type Paperback
Published in Oct 2016
Publisher Packt
ISBN-13 9781786466129
Length 392 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jon Hoffman Jon Hoffman
Author Profile Icon Jon Hoffman
Jon Hoffman
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Taking the First Steps with Swift FREE CHAPTER 2. Learning About Variables, Constants, Strings, and Operators 3. Using Swift Collections and the Tuple Type 4. Control Flow and Functions 5. Classes and Structures 6. Using Protocols and Protocol Extensions 7. Protocol-Oriented Design 8. Writing Safer Code with Availability and Error Handling 9. Custom Subscripting 10. Using Optional Types 11. Working with Generics 12. Working with Closures 13. Using Mix and Match 14. Concurrency and Parallelism in Swift 15. Swift Formatting and Style Guide 16. Swifts Core Libraries 17. Adopting Design Patterns in Swift

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.

You have been reading a chapter from
Mastering Swift 3
Published in: Oct 2016
Publisher: Packt
ISBN-13: 9781786466129
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime