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:
- Control flow
- Classes and structs
- Type (static) properties
Once you have the Playground opened, go through the material below before you get started.
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!
There are two main areas you need to worry about in the intro track playgrounds: navigation and the instructional & coding area.
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
- Go to
Xcode > Preferencesin the menu bar.
Text Editingin the popup. Check the box next to Line Numbers.
Creating a keyboard shortcut
Key Bindings(it's next to
executein the search bar.
- Double-click the white space next to
- Press and hold the
optionkey then press
- Click in the whitespace below.
- Verify the shortcut was added by watching this gif. The shortcut you added should match the gif.
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
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!
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
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!
Warnings do not stop your code from running. They are represented by a yellow triangle. Just like errors, they might have
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!
If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository or comment below.