Product College starts on September 5. Apply now.

Swift Intro

Swift Intro

April 17, 2017
Expand

Project: Getting Started with Swift 3 and Xcode 8

Download our Swift Language Playground here. Playgrounds are interactive coding environments that can contain instructions. You can think of it as a textbook you can code in!

Go through the playground and you'll get a great introduction to a bunch of programming concepts in Swift, including:

  1. var vs. let
  2. Types
  3. Strings
  4. Optionals
  5. Arrays
  6. Dictionaries
  7. Tuples
  8. Control flow
  9. Functions
  10. Classes and structs
  11. Initializers
  12. Type (static) properties
  13. Protocols

Once you have the Playground opened, go through the material below before you get started.

About Playgrounds

Before you get started, there are a few things you should know about Swift Playgrounds.

What are Playgrounds?

Playgrounds are interactive coding environments that can contain instructions. The code you write gets run every time you make a change and visual output is displayed on the right. This makes it a great way to learn how to code!

Playground interface

Playground interface

There are two main areas you need to worry about in the intro track playgrounds: navigation and the instructional & coding area.

Navigation

You can use this are to navigate directly to another page. The instructional and coding area also has next and previous page buttons at the bottom. If you do not see the navigation area, it can be opened with the button marked 1 in the above screenshot.

Instructional & coding area

This is where you'll be doing most of your work. This area allows for nicely formatted instructions and areas to code! Any areas that have line numbers next to them can be used for coding.

Setting up Xcode

There are two things we need to do to make our Xcode and Playground experience better. You should do this as soon as you start your first playground, it only needs to be done once!

Turning on line numbers

  1. Go to Xcode > Preferences in the menu bar. Preferences
  2. Click Text Editing in the popup. Check the box next to Line Numbers. Line numbers

Creating a keyboard shortcut

  1. Click Key Bindings (it's next to Text Editing).
  2. Enter execute in the search bar.
  3. Double-click the white space next to Execute Playgrounds.
  4. Press and hold the option key then press r.
  5. Click in the whitespace below.
  6. Verify the shortcut was added by watching this gif. The shortcut you added should match the gif.

Add shortcut

Running code in Playgrounds

By default, Playgrounds will run the code every-time you stop typing. The visualization will restart and be updated with your new code!

Re-running your code manually

If this does not seem to be working, you can re-run it by toggling the play/stop button at the bottom-left of your instructional & coding area. Toggle play

Slower computers

The auto-run feature might not work well on slower computers. You can turn it off by clicking and holding on the play/stop button then selecting Manually Run. Turn to manual

Now you can run your code when you are ready! By following the steps above or holding the option key and pressing r (this will only work if you setup the keyboard shortcut above).

My code is not working!

Playgrounds use a few different symbols to notify you when you have inputed code that will not work. Don't worry too much about the code in these examples, focus on the symbols presented.

It's good to understand what Xcode is trying to tell. Sometimes the message presented will be enough to help you fix it on your own. Other times it will be cryptic and you'll need to ask another student or instructor for help -- it is okay to ask for help!

Error

Error

Errors are a red octagon with a ! inside. Click them to read the message. You must fix the error for your code to finish running!

Error with suggestion

Error with suggestion

Errors with suggestions have a white circle inside instead of an !. When you double-click them, they pop up a Fix-it suggestion. You can double-click the suggestion and Xcode will try to automatically fix the problem. You must fix the error for your code to finish running!

Warning

Warning

Warnings do not stop your code from running. They are represented by a yellow triangle. Just like errors, they might have Fix-it suggestions.

Another thing to note in this example is some warnings and errors will have multiple messages associated with them. See how we click the grey 2 in the example to view both messages?

An important note

There is a ton of material in these playgrounds. We do not expect you to memorize it all! Go through each page and complete each challenge. We'll have time for group sessions to clear up any confusion and provide context for confusing concepts. If you can complete the challenges, you can move on to the next concept!

You'll have more practice with each of these concepts throughout the course. A lot of times, these concepts make more sense when you can see them in-action in the context of a real app or game! So don't worry, work through the playgrounds and be comfortable with the fact that you might not memorize everything the first time through. Read carefully, complete the challenges, and you'll do just fine!

Feedback

If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations across the USA and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more