LEARN SWIFT

Buy the PDF

1.1 A brief history of Swift

Ever since iOS was first released, the default way to develop apps for it was using Objective-C. There have been several other frameworks123 for creating apps that involve some variation of html/css/javascript embedded in an app container. But they all suffer from various problems including worse performance, limited functionality and general buginess. Building a native app is still the best way to go when it comes to Apple devices.

Then at Apples World Wide Developer Conference in 2014, Apple announced a new programming language: Swift, a language

“drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU …”4.

If you’ve been programming in one of those languages for that last few years, you’ve probably become accustomed to a lot of the conveniences that they provide. You’re used to doing high-level programming in a dynamic language. As such, objective-c was probably not a good fit for you. I’ve been programming professionally with Ruby and Python for over 10 years now. When the iPhone first came out I built a couple of apps for clients using Objective-C, but I didn’t enjoy the experience and I quickly went back to Ruby development. But that has changed with the release of Swift.

Swift 1.0 was released on September 9th, 2014 with version 6 of Xcode. With that release, a new era of development for Mac and iOS began. For anyone who has been programming in a modern language such as Ruby, Python, Clojure, Haskell5, etc, and who was put off developing iOS apps by Objective-C, Swift marks a new era. It’s now feasible to develop your apps almost entirely using Swift6.

At WWDC in 2015 Apple announced that Swift would be open sourced before the end of the year. Details were sketchy but in December 2015 they followed up as planned and released Swift as open source. The Swift open source release included the full source code for the language, binaries for Mac OS and Linux and an early version of a Swift package manager. Also it introduced a community process for discussion of future language evolution. It looks like Swift will be developed in the open from this point on.

This is a huge milestone for Swift development. The fact that it’s open source and can run on linux opens it up to whole new areas of applications. I personally look forward to being able to build and deploy web applications in Swift that can be deployed on a Linux server environment. The next few years will be an exciting time for Swift developers.

Swift is a wonderful language, that combines many of the benefits of modern dynamic languages with the benefits of a strong type system, in a language that produces fast code. It’s a joy to program in. And with it’s release, I think we’ll see a lot of programmers who previously avoided programming for Apple hardware become more interested in coding for that platform. In short, the jump from languages like Ruby and Python to Swift is much smaller than the jump from those languages to Objective-C.

1.2 A high level view (language features)

Swift is in the main an Object-Oriented language with some interesting features inspired by functional-style languages. Here are some of the highlights:

  • c-like syntax.
  • Type safe. It’s strongly, staticly typed and checks types at compile time. It uses type inference so you don’t need to declare types for everything.
  • Supports lots of data-types natively, including lists, dictionaries, tuples, enumerations, structs and classes.
  • Uses automatic reference counting for memory management.
  • Strong support for closures. Functions are first-class objects.
  • Supports interactive programming and scripting using repl and playgrounds.
  • Compiles to fast native code.
  • Object-Oriented: classes, protocols (similar to java interfaces), extensions (similar to ruby mixins).
  • Generics for generic programming.
  • Some limited support for pattern matching.
  • Functional. There’s enough functional goodness in Swift that you can eschew OO and program in a functional style if you want to.

1.3 What lies ahead

Each chapter focuses on a specific feature of the language. I’ll assume you already know how to program and so I won’t go into great detail of various programming concepts. Instead I’ll assume understand the basic language constructs from other programming languages and just show how various language constructs are implemented in Swift.

1.4 Who this book is for

This book is for people who can already program in another language and are intrigued by Swift and want to learn it. It will outline all the various language features are implemented in Swift.

1.5 Swift version

This book has been updated to Swift 2.1. All code in this book should work with Swift 2.1.

1.6 Copyright and Licence

Learn Swift. Copyright (c) 2015 by Aidan Finn. All rights reserved.

All source code in the book is available under the MIT License.

  1. http://www.appcelerator.com/ 
  2. http://phonegap.com/ 
  3. React Native was introduced at React.js Conf 2015. It looks interesting but it’s too early to tell how it compares to native development and what compromises are involved in using it. 
  4. http://nondot.org/sabre/ 
  5. I mention these languages specifically because I think practitioners in those languages will find a lot to like in Swift and will find the transition relatively easy. Specifically, if you tend towards using a functional-style when programming in those languages you’ll find that many of the idioms you are used to can be translated to Swift. The jump from these langyuages to Swift is much less painful than the jump from these languages to Objective-C. 
  6. While it’s possible to entirely develop your app in Swift, you’ll still be making heavy use of the Apple Cocoa libraries. These have been heavily incluenced by years of Objective-C development so using them from Swift can often be somwhat ungainly.