Preface: Swift for the Rest of Your Application

Q: Why did the human put on his boxing gloves?

A: He had to punch some cards.

Today’s applications do not run on a single platform. Rather, some parts run on resource-limited devices, and other parts run on a vast and mysterious cloud of servers. This separation has led to a schism in how we build these applications because different platforms have different requirements: the mobile portions must conserve battery power, while the server portions must handle a large number of requests simultaneously. Consequently, programmers use different languages for different parts of applications—for instance, JavaScript for the browser, and Java for the server.

However, constructing an application out of multiple languages is fraught with drawbacks: different teams in the same organization speak different languages—literally—and must master different developer ecosystems. Precious time must be spent translating concepts across language barriers and a few developers must know all of the languages in order to be effective. Test cases and data models must be replicated in different languages, introducing bugs and incurring future maintenance efforts. Because third-party libraries cannot be shared across groups, each team must learn different APIs to obtain merely the same functionality.

​Swift was introduced by Apple in 2014 and replaced Objective-C as the recommended language for all new applications running on Apple devices. Later, when Swift became open source in 2015, it spread to new platforms. Currently, Swift is available on x86, ARM (including Raspberry Pi), and zOS architectures, as well as Linux, macOS, tvOS, watchOS, and iOS operating systems. So, it is now possible to write a whole end-to-end mobile application—front-end, middle, back, and even toaster—all in Swift. That’s why we wrote this book; we wanted to help you, the developer, who is most likely writing in Java or JavaScript, to consider a switch to Swift.

Why adopt Swift?

  • The Swift language may well be better than what you are currently using. 
  • You can develop and debug in a consistent environment. Integrated development environments (IDEs) offer a tremendous amount of functionality such as text editing, static analysis, code completion, debugging, profiling, and even source-control integration. Switching back and forth between say, Eclipse and Xcode is a bit like switching between French horn and electric guitar: neither easy nor productive.
  • You can reuse code. When each bit of functionality is expressed exactly once, there is less work, more understanding, and fewer bugs.
  • You can leverage Swift’s features—such as optional types, value types, and functional programming facilities—to detect many bugs at compile time that would otherwise be hard to find. 
  • Since Swift uses the LLVM compiler toolchain for producing native-code binaries, your applications have the potential for competitive performance in terms of speed, startup time, and memory usage. However, examination of performance is outside the scope of this book.
  • You will find an active and approachable community of Swift developers who are creating web posts, books, and videos. In 2016, Swift was cited as the second “Most Loved” language in a StackOverflow survey, and the third most upward trending technology.  

This book will introduce you to the Swift language, illustrate some of its most interesting features, and show you how to create and deploy a simple web service. Let’s get started!

Coding Style & Implementations

In the examples, the space constraints of this medium have led us to indent, break lines, and place brackets differently than we would in actual code. In addition, space has precluded the inclusion of full implementations and blocks of code in this edition contain inconsistencies in color and font. If the inconsistencies confuse you, please consult the repositories in Table P-1.

Table P-1. Where to find code examples
Repository name Referenced in
Book snippets Code snippets from the book
MiniPromiseKit Created in Chapter 3; used in Chapter 5
Pipes Used in Chapter 4
Kitura To-Do List Created in Chapter 5

Acknowledgments

This book would not have been possible without the support, encouragement, and guidance of the IBM Cloud and Swift@IBM leadership team, including Pat Bohrer, Eli Cleary, Jason Gartner, Sandeep Gopisetty, Heiko Ludwig, Giovanni Pacifici, John Ponzo, and Karl Weinmeister. In addition, we want to extend our thanks to the many IBM Swift engineers and Swift community members working to bring Swift to the server—including Chris Bailey, Hubertus Franke, David Grove, David Jones, and Shmuel Kallner—for sharing their collective technical insights and creating the tools and libraries described herein. The Swift community’s embrace of Swift on the server reassured us that our contribution would be valued. The growing number of their instructive blog posts, videos, conference talks, and books have been of great help. We would like to thank our technical reviewers: Chris Devers, Shun Jiang, and Andrew Black. Nan Barber and the O’Reilly team had the daunting task of editing our lengthy technical drafts and producing this book.

We owe a huge debt of gratitude to the Apple Core Swift Team for their courage, intelligence, talent, wisdom, and generosity for bringing a new language and ecosystem into existence and moving it to open source. Language design involves many difficult and complex tradeoffs, and bringing a new language to the world requires a tremendous amount of work. The rapid acceptance of Swift by developers is powerful testimony to the quality of the language.

Words fall short in plumbing the depths of our gratitude for the support and love of our sweethearts, Barbara Hurd and Valerie Magolan.

Get Extending Swift Value(s) to the Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.