Ever since iOS was first released, the default way to develop apps for it was using Objective-C. There have been a several other frameworks 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 .
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, Haskell.
If youve been programming in one of those languages for that last few years, youve probably become accustomed to a lot of the conveniences that they provide. Youre used to doing high-level programming in a dynamic language. As such, objective-c was probably not a good fit for you. Ive 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 didnt enjoy the experience and I quickly went back to Ruby development. But that has changed with the release of Swift.
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. Its a joy to program in. And with its release, I think well 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.
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. Its strongly, staticly typed and checks types at compile time. It uses type inference so you dont 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. Theres enough functional goodness in Swift that you can eschew OO and program in a functional style if you want to.
Each chapter focuses on a specific feature of the language. Ill assume you already know how to program and so I wont go into great detail of various programming concepts. Instead Ill assume understand the basic language constructs from other programming languages and just show how various language constructs are implemented in Swift.
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.
Learn Swift. Copyright (c) 2015 by Aidan Finn. All rights reserved.
All source code in the book is available under the MIT License.
- http://www.appcelerator.com/
- http://phonegap.com/
- React Native was introduced at React.js Conf 2015. It looks interesting but its too early to tell how it compares to native development and what compromises are involved in using it.
- http://nondot.org/sabre/
- 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 youll 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.
- While its possible to entirely develop your app in Swift, youll 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.
The first thing you need to do with any new language is figure out how to get some code running. In this chapter well create a simple hello world program and run it in Swift. Well go through multiple ways of running a program: interactively at the REPL, interactively using playgrounds, and as a standalone executable program (using both Xcode and the swiftc
command).
Figure 2.1: Installing Xcode using the App Store.
To get started youll need to have the latest version of Xcode installed (At the time of writing the latest version is 6.1). You can install Xcode through the Mac App Store (See ). To install it open up the app store, search for Xcode, and then install it. Its quite a large download so you may want to start the install now and go get a cup of tea.
A REPL is a read-eval-print-loop, and is a way of interactively running and inspecting code. If youve come from a dynamic language such as Ruby, Python or Clojure the REPL probably forms an important part of your development workflow. Youll often use it to try out libraries, debug existing code and inspect whats going on in your program.
Swift also has a REPL, although youll find it quite a bit different to the REPL youre used to (For playing around with Swift code, playgrounds - covered in .
You can run the Swift REPL from the command line and enter Swift code directly into it.
$ swiftWelcome to Swift! Type :help for assistance. 1> println ( "Hello world" ) Hello world 2> :quit
Whenever you enter valid Swift code in the REPL, it will immediately compile and run it.
Lets play around with the Swift REPL a bit.
$ swiftWelcome to Swift! Type :help for assistance. 1> 3.14 * 2.5 * 2.5 $R0 : Double = 19.625 2> println ( "The area of the circle is \($R0)" ) The area of the circle is 19.625 3> 4> $R0 * 2 $R1 : Double = 39.25 5> println ( "And double that is \($R1)" ) And double that is 39.25 6>
You can access previous lines that youve entered using $R0
etc. Each result in the REPL gets assigned to a $RN
variable so you can later refer back to that result.
You can enter multi-line code and the REPL wont evaluate it until the code block is finished.
$ swiftWelcome to Swift! Type :help for assistance. 1> 3.14 * 2.5 * 2. 2.5 $R0 : Double = 19.625 3>
If we make a mistake when we enter our multi-line code, then we can edit the multiple lines as a single block. Here we enter a function to double an integer thats given as an argument, but we incorrectly declare the return type (well get to functions later, but for now just focus on the ability to correct multiple lines at once.)