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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Swift

You're reading from   Mastering Swift

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781784392154
Length 358 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 (17) Chapters Close

Preface 1. Taking the First Steps with Swift FREE CHAPTER 2. Learning about Variables, Constants, Strings, and Operators 3. Using Collections and Cocoa Data Types 4. Control Flow and Functions 5. Classes and Structures 6. Working with XML and JSON Data 7. Custom Subscripting 8. Using Optional Type and Optional Chaining 9. Working with Generics 10. Working with Closures 11. Using Mix and Match 12. Concurrency and Parallelism in Swift 13. Swift Formatting and Style Guide 14. Network Development with Swift 15. Adopting Design Patterns in Swift Index

What is Swift?

Swift is Apple's new programming language that was 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.

Swift can be thought of as Objective-C reimagined using modern concepts and safe programming patterns. In Apple's own words, Swift is like Objective-C without the C. Chris Lattner, the creator of Swift, said Swift took language ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.

At WWDC 2014, Apple really stressed that Swift was Safe by Default. Swift is designed to eliminate many common programming errors, making applications more secure and less prone to bugs. As we look at Swift throughout this book, we will point out many ways in which Swift is safer than not only Objective-C, but also safer than most other modern languages.

The development of Swift 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 at Apple in the summer of 2005. He has held several positions in the Developers Tools group and is currently the Director and Architect of that group. On his home page (http://www.nondot.org/sabre/), Chris notes that Xcode's Playground (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 the book as a test and experimentation platform.

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. Swift also provides seamless access to existing Cocoa frameworks. This gives Objective-C developers a certain amount of familiarity when they begin to learn Swift.

While there are a lot of similarities between Swift and Objective-C, there are also 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 alright 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, and eliminating bugs such as the Goto Fail bug in Apple's SSL library.

Mix and match allows us to create applications that contain both Objective-C and Swift files that can communicate with each other. This allows us to systematically update current Objective-C applications with Swift classes. It also allows us to use current Objective-C libraries/frameworks in our Swift applications.

Tip

Mix and match lets Objective-C and Swift files coexist in the same project. This allows us to begin using Swift without throwing away our existing Objective-C code base or projects.

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

If you are an iOS or OS X developer and you are still not convinced that learning Swift is a good idea, then maybe this one paragraph from Apple's Swift page (https://developer.apple.com/swift/) will help convince you:

Swift is a successor to the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.

The first line in that paragraph that says, "Swift is a successor to the C and Objective-C languages" is the most important line. This line and other Apple documentation tells us that Apple sees the Swift language as its application and systems programming language of the future. While Objective-C is not going away anytime soon, it sounds like it will be taking a backseat to Swift in the very near future.

Swift features

When Apple said that Swift is Objective-C without the C, they are 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 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 maintains the C compatibility.

Since Swift does not need to maintain the same C compatibility of 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#, 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.

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 a 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.

Multiple return types

Functions can have multiple return types using tuples.

Operator overloading

Classes can provide their own implementation of existing operators.

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
Published in: Jun 2015
Publisher: Packt
ISBN-13: 9781784392154
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
Banner background image