A Better Way to Learn Swift

Become a Subscriber

No looking back now.

You’ve come to this tutorial because you want to learn Swift, Apple’s new programming language for making iOS and MacOS apps. This tutorial is bringing together various resources to swiftly (ha!) and thoroughly introduce each component of the Swift Programming Language.

There was an initial frenzy of excitement around the new language in June when Apple introduced Swift at WWDC 2014. Since then, the various beta bugs and the fact that no devices supported apps built in Swift has led to a bit of a decline in Swift’s popularity. Well Swift is now in release 1.0 and iOS8 will soon be in the hands of millions of developers and consumers alike. There has never been a better time to dive into Swift.

Looking to learn how to build a Swift app from the ground up? Check out the Flourish Tutorial!

A note on this tutorial

Swift is in its infancy. This tutorial is going to evolve and change as we learn more about the language and the applications of the concepts we cover. We are also constantly looking for more and better resources for each topic. With that in mind, feel free to comment or email us if you find good Swift resources that aren’t mentioned here, or something here that is out of date.

Prerequisites

To work through this tutorial you’ll need a fully functioning Apple developer setup. This includes:

A mac running OSX Mavericks or Yosemite

An Apple developer account

A copy of Xcode

Resources

We expect a plethora of learning resources to come out for Swift now that the language is out of beta. Luckily, many resources are already available and this heavily leverages the following (and they are all free!):

The Swift Programming Language ebook from Apple (also available on iBooks)

Apple’s Introduction to Swift

The MadApper Swift video series

The Skip Wilson Swift video series

One Last Thing

This entire tutorial can be done in a playground: an interactive environment that exists in xcode that allows you to code and receive immediate results from your code without having to compile an app. Before actually learning about Swift, learn how to create and navigate around playgrounds.

Read Exploring and Evaluating Swift Code in a Playground

Swift Basics

Now you are going to dive into the language itself. The goal of this section is to get a handle on the syntax, operators, and data types in Swift.

Swift Syntax

When learning a new language syntax, it is recommended that you immediately start writing in the language as much as possible. Apple’s “A Swift Tour” chapter of The Swift Programming Guide provides an overview of Swift Syntax as well as a set of “experiments”, short exercises that let you write some Swift code focusing on a different aspect of the Swift Syntax.

Variables and Constants

Readings

Along with the experiments in the Swift ebook, we are including two blog posts about reserved words and operators that can serve as reference materials.

Read the A Swift Tour chapter in the Swift ebook and work through the experiments.

Check your solutions to experiments and make any corrections.

Read about Swift Operators

Read about Swift Reserved Words. Don’t worry about memorizing these, just be aware.

Optional: Swift for Javscript Developers - We know many of you are coming from Javascript. Once you look a the Swift syntax you’re going to see some similarities, so it is worth it to view the video and blog post by JP Simard on Swift for Javascript developers. JP goes over the key differences between the two languages as well as some similarities.

Swift Types

If you’ve made it this far you’ve read that one of the good parts about Swift is that it is a type-safe language. Even without having any idea what that means, you can probably assume the section in this tutorial about types is important. For learning about types, the MadApper video series and Apple’s documentation pair nicely. Types in Swift can be confusing because there are four categories of types: named types, compound types, value types, and reference types. Each type in Swift is a compound type or a named type as well as either a reference type or a value type. Confused yet? No worries, we’ll sort all of this out.

Pre-Readings

For Types, do the readings first before watching the videos on the specific types.

Value Types vs. Reference Types

Building Intuition for Value Types vs. Reference Types

Compound Types vs. Named Types

Videos

Booleans

Strings

String Manipulation

Numbers: Integers, Floats, Doubles

Arrays

Array Manipulation

Dictionaries

Dictionary Manipulation

Extensions

We can take all of our knowledge of the various data types in Swift and give them new functionality. Extensions allow us to give types new methods to suit the needs of our applications. For this, watch a long and awesome video on creating an extension and don’t forget only named types can be extended.

Extensions

Readings

Read the Extensions chapter in the Swift book.

Control Flow

Okay now things get exciting. We are going to cover optionals, loops, functions, and closures. These concepts allow us to allow our code to do things only under certain conditions. Armed with this knowledge and what you’ve learned so far, you will be able to write code more concisely and you’ll even be able to write the logic for basic games.

Loops and Conditionals

Control flow constructs in Swift allow us to iterate over collections, loop through a chunk of code multiple times, and execute code based on conditional statements. The MadApper series does a good job of covering these concepts

For (condition, increment) Loops

For In Loops

While Loops

Do While Loops

If

Switch

Optionals

Optional are a concept that is introduced in Swift and has no parallel in C or Objective-C. Using them effectively can prevent some common programming errors and increase the type safety of your code.

The Basics

Readings

Read the section called “Optionals” in “The Basics Chapter” of the Swift ebook.

Read The Complete Guide to Understanding Swift Optionals to complete your understanding of optionals. Learn how they work under the hood, best pratices, and why we need them in the first place.

Read “How Swift "Defines Away” Common Programming Errors…sort of" by Matt Bridges

Read “Optional Case Study: Value for Keys” from the Swift Blog

Functions and Closures

Functions and Closures are self-contained blocks of code functionality. That is a vague explanation because functions and closures have very broad uses and can take many forms. For this section, jump in with two long (and excellent) video tutorials from Skip Wilson to get a practical crash course.

Functions

Closures

Functions and closures can take many forms in Swift. The difference between a function and a closure can also be hard to grasp. The videos cover the basics, but these readings will give you a more complete understanding.

Read the Functions chapter in the Swift ebook.

Read the Closures chapter in the Swift ebook.

Read A Basic Tutorial on Functions and Closures in Swift for clarification on functions, closures, and closure expressions.

For reference, read The Many Forms of Swift Functions to see all of the various form functions can take in Swift.

Finally Enough About Swift Closures to Choke a Horse shows the flexibility of Swift in writing closures.

Object Oriented Programming

Objective-C is an object-oriented programming language. Swift preserves many of the object-oriented ideas and constructs from Objective-C and you’ll need to be comfortable with these concepts to write apps in Swift.

Structs

Enums

Classes

Protocols

Readings

Methods, Properties, and Initializers were covered in the videos, but the Swift ebook is a good place to get a detailed explanation of each of those ideas.

Properties

Methods

Inheritance

Initializers

Now for a few practical readings on Swift objects

When to use Enums, Structs, and Classes

OOP and Functional

Testing

Testing is important to driving the design of our applications, ensuring they behave as we expect, and documenting our code.

Watch Adam Leonard’s Swiftly Testing with Quick

Watch WWDC Testing in XCode 6

Readings

Introduction to XCTest

Unit Testing in Swift

Swift Unit Testing Tips and Tricks

XCTest

Asynchronous Unit Testing

Bad testing Practices

BDD Style Testing with Slepnir

Meet the Gang

There is an array of libraries to use for testing. Whether your flavor is BDD or just plain old TDD there is something for everyone!

  • Sleipinr is a BDD style testing framework for Swift.
  • Quick is another BDD style testing framework for Swift.
  • Nimble is a compliment to Quick. It’s a Matchers framework.

SDK’s

When it comes to actually building apps in Swift, it is paramount to get an understanding of the various SDKs provided by Apple to get a sense for all of deep functionality that Apple provides for developers and to prevent yourself from trying to write code for functionality that already exists in an SDK or library. In fact, we recommend always looking for a trusted library before embarking on writing your own functionality (except, of course, if you’re writing it yourself to better learn Swift).

Sprite Kit

Sprite Kit gives developers the ability to create simple or advanced 2D games by providing animation and physics functionality.

Readings

Introduction to Sprite Kit

Sprite Kit Animation

Sprite Kit Physics

Sprite Kit Particles

Cheat Sheets

Tools, tricks, and tips for both novice and senior Swift developers.

Readings

Master List of Swift Cheat Sheets showcases a few cheat sheets for Swift.

Swift Syntax Guidelines is a best practices post in that is periodically updated as the language matures and best practices emerge.