Skip to content

Latest commit

 

History

History
498 lines (334 loc) · 18.2 KB

a-better-way-to-learn-swift.mdown

File metadata and controls

498 lines (334 loc) · 18.2 KB

Introduction

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.

{announcement} 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, you can find the raw markdown for this tutorial over at Github. If you find good Swift resources that aren't mentioned here, feel free to submit a pull request!

We'd like to give a special thanks to the great folks over at Swiftcast for kick-starting this tutorial. If you have any questions or comments tweet them at @SwiftCastTV!

Prerequisites

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

{x: running_osx} A mac running OSX Mavericks or Yosemite

{x: apple_dev} An Apple developer account

{x: copy_of_xcode} A copy of Xcode 6

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)
  • 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.

{x: 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.

{x: variables_constants} 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.

{x: chapter_tour} Read the A Swift Tour chapter in the Swift ebook and work through the experiments.

{x: solutions} Check your solutions to experiments and make any corrections.

{x: operators} Read about Swift operators

{x: reserved_words} Read about Swift Reserved Words. Don't worry about memorizing these, just be aware.

{x: swift_js} 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.

{x: values_vs_reference} Value Types vs. Reference Types

{x: values_vs_reference_intuition} Building Intuition for Value Types vs. Reference Types

{x: compound_vs_named} Compound Types vs. Named Types

Videos

{x: booleans} Booleans

{x: strings} Strings

{x: strings_man} String Manipulation

{x: numbers} Numbers: Integers, Floats, Doubles

{x: arrays} Arrays

{x: arrays_man} Array Manipulation

{x: dict} Dictionaries

{x: dict_man} 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.

{x: extensions} Extensions

Readings

{x: chapter_extensions} 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

{x: for} For (condition, increment) Loops

{x: for_in} For In Loops

{x: while} While Loops

{x: do_while} Do While Loops

{x: if} If

{x: switch} 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.

{x: chapter_basics} Optionals

Readings

{x: optionals_basics} Read the section called "Optionals" in "The Basics Chapter" of the Swift ebook.

{x: optionals_complete_guide} 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.

{x: swift_defines_away} Read "How Swift "Defines Away" Common Programming Errors…sort of" by Matt Bridges

{x: optionals_case} 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.

{x: functions} Functions

{x: closures} 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.

{x: chapter_functions} Read the Functions chapter in the Swift ebook.

{x: chapter_closures} Read the Closures chapter in the Swift ebook.

{x: basic_functions_closures} Read A Basic Tutorial on Functions and Closures in Swift for clarification on functions, closures, and closure expressions.

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

{x: closure_choke} 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.

{x: structs} Structs

{x: enums} Enums

{x: classes} Classes

{x: protocols} 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.

{x: chapter_properties} Properties

{x: chapter_methods} Methods

{x: chapter_inheritance} Inheritance

{x: chapter_initializers} Initializers

Now for a few practical readings on Swift objects

{x: when_to_use_enums} When to use Enums, Structs, and Classes

{x: oop_functional} OOP and Functional

Testing

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

{x: adam_quick} Watch Adam Leonard's Swiftly Testing with Quick

{x: WWDC_testing} Watch WWDC Testing in XCode 6

Readings

{x: xctest} Introduction to XCTest

{x: unit_testing_reading} Unit Testing in Swift

{x: tips_tricks} Swift Unit Testing Tips and Tricks

{x: async_unit_testing} XCTest

{x: async_unit_testing} Asynchronous Unit Testing

{x: bad_testing} Bad testing Practices

{x: bdd_testing} 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

{x: sprite_intro} Introduction to Sprite Kit

{x: sprite_animation} Sprite Kit Animation

{x: sprite_physics} Sprite Kit Physics

{x: sprite_particles} Sprite Kit Particles

Cheat Sheets

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

Readings

{x: cheat_sheet} Master List of Swift Cheat Sheets showcases a few cheat sheets for Swift.

{x: syntax_guide} Swift Syntax Guidelines is a best practices post in that is periodically updated as the language matures and best practices emerge.