INTRODUCTION
THE POPULARITY OF IOS has drawn many programmers to Apples platforms. Software on both OS X and iOS has long been written in Objective-C, a language first developed in the 1980s that is best described as an amalgamation of C and Smalltalk. Objective-C lacks many features that modern programmers expect to find in a programming language. Over the last ten years, Apple has adapted and improved Objective-C, but many programmers still find it to be an alien language.
Enter Swift. Swift is a modern programming language, built with knowledge gained in the last 30 years of programming language research. Developed by the same researchers who developed clang, the modern compiler toolchain used by Apple to build OS X and iOS software, Swift incorporates many features that are expected of a modern programming language. It sports a strong type system to prevent many of the mistakes prevalent in both C and Objective-C programming. It has a refined class system complete with static and computable properties. It supports closures and treats functions as first-class objects, allowing them to easily be saved as variables or passed to functions and methods as parameters. As you read this book, youll find many more advanced features that are present in Swift as well.
Swift has been designed from the ground up to be a modern programming language suitable for both systems and application development on Apples platforms. Apple has already thrown a lot of support behind Swift, and the language is constantly being improved. Swift represents the future of software development on iOS and OS X, and programmers working on that platform should expect Swift to occupy an increasingly dominant position on those systems.
WHO THIS BOOK IS FOR
This book is aimed at software developers with experience writing Objective-C applications on OS X or iOS. Some knowledge and experience with Swift are recommended but are by no means necessary to understand the material presented in this book. The book introduces the key concepts of Swift that may be new even to advanced iOS and OS X programmers. Over the course of the book, several small projects are used to illustrate the concepts as they are introduced. The text also makes extensive use of playgrounds, a new feature of Xcode that allows you to write and interact with Swift code without the overhead of an entire Xcode project.
The first chapter is an overview of Swifts syntax and semantics but does not cover any advanced features of the language. The second chapter covers the use of Xcode, including playgrounds. If you already have some familiarity with Swift and Xcode, you may want to skip the first and second chapters and get started immediately with Chapter 3.
WHAT THIS BOOK COVERS
This book first offers a primer to the Swift programming language, released by Apple in June 2014. It quickly moves on to more advanced Swift programming topics, including the new playgrounds feature in Xcode; classes, structs, and enums; concurrent programming; and advanced features of Swifts type system. It also covers practical topics that you will deal with on a regular basis as a Swift programmer, including interfacing with web services using JSON, Core Data, and using C and Objective-C code in your Swift project. It shows some of the advanced features of Xcode, including the use of the debugger built in to Xcode, and discusses the lower-level details of the Swift and Objective-C runtimes. And because it is impossible to completely avoid C when writing software on OS X and iOS, an index provides a quick overview of the C programming language.
Swift has been constantly evolving since its release in June 2014. This book covers version 1.1.
Because Swift is still changing, you may find some minor differences between the version discussed in this book and the latest released version. However, the vast majority of material presented in this book will still be valid for future versions of Swift.
HOW THIS BOOK IS STRUCTURED
This book is divided into two sections. The first section provides an introduction to the Swift programming language and covers many of the more practical topics you will encounter as a Swift programmer. The second section is a deep dive into the intricacies of Swift.
- Chapter 1: Reviews the syntax and semantics of the Swift programming language. It is useful for those programmers who are new to the language or want a quick refresher.
- Chapter 2: Covers the Swift-related changes to Xcode. It introduces Xcodes new feature, playgrounds, which provide a way to interactively work with Swift code.
- Chapter 3: Covers Swifts classes, structs, and enums in greater detail than Chapter 1, providing a solid foundation for object-oriented programming in Swift.
- Chapter 4: Discusses concurrent programming in the context of Swift and the Foundation framework on iOS and OS X.
- Chapter 5: Shows you how to communicate with remote web services using JSON.
- Chapter 6: Discusses how to use Core Data to store and search for information used by your program.
- Chapter 7: Shows you how to extend classes using protocols and class extensions.
- Chapter 8: Covers Swifts type system in fine detail and shows you how to take advantage of Swifts strong type system to write less error-prone code.
- Chapter 9: Shows you how you can mix C and Objective-C code in your Swift programs.
- Chapter 10: Introduces the debugger and shows you how you can track down and fix problems in your code.
- Chapter 11: Discusses the runtime architecture of both Swift and Objective-C and demonstrates how programs are loaded and executed on iOS and OS X.
- Appendix: Covers the features and concepts that the C programming language introduces.
If you are already familiar with Swift and Xcode, you may want to skip Chapters 1 and 2 and start right in on Chapter 3.
WHAT YOU NEED TO USE THIS BOOK
To run the samples in the book, you will need the following:
- A Mac running OS X 10.9 or later
- Xcode 6
- An iPhone, iPod Touch, or iPad if you want to run the examples on an actual device instead of the iOS simulator that ships with Xcode
The source code for the samples is available for download from the Wrox website at: www.wrox.com/go/proswift
.
CONVENTIONS
To help you get the most from the text and keep track of whats happening, this book uses a number of conventions.
WARNINGWarnings hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.
NOTENotes indicate notes, tips, hints, tricks, or asides to the current discussion.
As for styles in the text:
- We highlight new terms and important words when we introduce them.
- We show keyboard strokes like this: Ctrl+A.
- We show file names, URLs, and code within the text like so:
persistence.properties
.
We present code in two different ways:
We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.SOURCE CODE
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com
. Specifically for this book, the code download is on the Download Code tab at www.wrox.com/go/proswift