Programming in Swift: Fundamentals

Oct 19 2021 Swift 5.5, iOS 15, Xcode 13

Part 1: Core Concepts

2. Swift Playgrounds & Comments

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 1. Introduction Next episode: 3. Booleans & Comparison Operators

Update Notes: The student materials have been reviewed and are updated as of October 2021.

If you finished building the Bull’s Eye game in Your First iOS App, then you’re already familiar with using Swift in Xcode’s standard editor. Xcode has another interface called “Playgrounds” that you’ll be using in this course, so let’s get familiar with how they work.

And you can see results immediately as you type, rather than having to build and run an app to see what happens every time you make a change.

A Playground can also be a powerful tool to experiment with new things as you learn more in your Swift and iOS development career.

In this course, you’ll be using Playgrounds to dig deeper into programming in Swift. This way, you can easily focus on the core concepts of the Swift language, like functions and classes, without getting bogged down worrying about UI like buttons, labels, and sliders.

  • To create a new playground in Xcode, go to File - New Playground. There’s also a keyboard shortcut, Shift-Option-Command-N.

  • There are templates for playgrounds, just like for full Xcode projects. I just want a blank one.

  • Name your playground

  • and save it wherever you’d like. And there’s your first playground!

You can also open existing playgrounds in Xcode. I’ll have you do that now, and I’ll continue the tour through playgrounds once you have it open.

Simply download the playground that comes packaged with the materials in this course.

You can use the File > Open menu option, or you can simply double-click on the playground file itself, like I’ll do here:

and it will open up in Xcode. Okay! Now you’re ready to continue on with the tour of xcode.

  • The main area, here, is called the source editor.

This is where you’ll write your Swift code. It works similar to the editor in an Xcode project, like you used to edit Swift code for Bull’s Eye.

There are few differences - If you started with the blank template, you only have one file in your playground, instead of the multiple files you get in an Xcode project.

Playgrounds can have more than one file, too, but we’ll get to that later in the course.

  • Another difference you might notice is the results sidebar on the right.

This is what makes Playgrounds special and really sets them apart from Xcode’s standard code editor.

Playgrounds can run your code constantly, as you add it, and you’ll see the results of each line of code in the sidebar.

  • So right now, without having to touch anything, the playground has already run the starter code, and you can see that this variable, str,
  • has been successfully declared and set to a value of “Hello, playground”
  • I don’t really like using abbreviations for variable names so I’m going to change this str to welcomeMessage
var welcomeMessage = ...
  • Down at the bottom of the Playground is the execution control.

As you saw, Swift Playgrounds execute automatically by default, meaning you can write code and immediately see the output.

This control allows you to execute the playground again, if you want, just by clicking.

You can choose to switch between automatic execution and manual execution modes if you hold down this button.

  • All the way at the top of the Playground is the activity viewer, which shows the status of the playground.

For example, right now it shows that the playground has finished executing and is ready to handle more code in the source editor.

When the playground is actively executing, the viewer will display a spinner. You can see that for yourself if you type some code into the editor.

  • Go ahead and add another line of code to the playground. I think we need a goodbye message as well, so I’ll add a constant goodbyeMessage and set it to the value of “See you soon!”.
let goodbyeMessage = "See you soon!"
  • …and you’ll see that spinner in action.

You can also see the results of both lines of code over in the sidebar!

If this is your first time using a playground, it might look like the results sidebar is like the debugging console, where your print statements appear.

That’s not quite the case, though — let me print out goodbyeMessage, to show you:

print(goodbyeMessage)

But now you can see they’re separate things! Notice you can see the results of each line of code in the sidebar,

including the print statement, but the console only shows the print statement.

The console popped up on its own, because I printed something to it, but you can show or hide the console any time you’d like.

There’s a button to toggle the console in the upper right, or you can use the keyboard shortcut: Shift-Command-Y

You can think of Playgrounds like a scratch pad for trying out Swift code. I use them to test out code all the time.

Now, when you write Swift code, it’s really common to want to document what you’re doing, for yourself or someone you’re working with. Or maybe you want to add a note to remind yourself do something later. Or maybe you just want to put some dividers in there with some text to help organize your code.

But these things aren’t code; so how can you tell the Swift compiler to ignore these things? Basically, you want to add text that the Swift compiler will ignore.

A comment is exactly that - text that is ignored by the compiler.

When you worked on the Bull’s Eye app, you may have noticed a few comments in the code and wondered what they were.

So let’s take a look at how add comments, in Swift!

  • To add a comment, it’s as simple as typing two forward slashes
//

and then type whatever comment you want to add.

// The code below prints out a goodbye message
  • You can tell it isn’t executed, because your playground is still compiling! You still see that “Hello, playground”, so we know the playground is running as expected.

  • You can just keep adding lines and lines of comments like that…

// Yet another comment.
  • But if you want to write comments that span multiple lines, there’s an easier way. Start with a forward-slash asterisk…

  • Add your comment…

/* This is a comment
   spread out over a few lines
   also, a haiku! 
  • and finish it off with a asterisk and a forward-slash.
*/

There’s another really common use for comments, and that’s when you have code that you want to stop from executing, just temporarily, but that you don’t want to delete and lose it.

Let’s say you don’t necessarily want to print put this goodbyeMessage variable anymore, but you want to keep this line of code around temporarily for some use in the near future.

  • Move your cursor anywhere on that line of code, and type two slashes; this is known as “commenting-out” your code.

//print (goodbyeMessage)

This comments out the entire line, and you should see the result dim or disappear in the sidebar. It’s no longer being executed.

You can also use the keyboard shortcut Command - Forward-slash to add a comment in front of a line of code:

//let goodbyeMessage = "See you soon!"
  • If you change your mind, you can use the same shortcut, Command - Forward-slash, to un-comment the code.

If you comment out code, you should only do so temporarily! Remember to delete any unused or unnecessary code from your projects. Commented out code that’s left hanging around in your project is only going to confuse you in the future, as well as anyone else working on projects with you.

There’s one last thing I want to show you about comments; and that’s a special feature of playgrounds that let you put special markup code that looks and works differently from the rest of your Swift code.

  • You may have wondered about these “Previous” and “Next” lines at the top and bottom of the code.

If you click on them, they’ll take you to the previous and the next playgrounds in this file, respectively. But if you go to Editor > Show Raw Markup, you’ll see that these are a special type of comment in Xcode known as “Documentation Markup”.

The two forward slashes, followed by a colon, tell Xcode “Hey, the code in here is written in something called Markdown”, and Xcode will display this differently from the rest of the code.

Markdown is a simple language that lets you format text and add things like links; you don’t need to know Markdown for this course, and I don’t expect you to create any comments like that, but I figured you would like to know how those links got at the top and the bottom of the playgrounds.

Select Editor > View Rendered Markup to get back to the normal view of your playground.

That’s the end of your brief tour of playgrounds. Head on into the next section, where you’ll start learning about Booleans and comparison operators. I’ll see you there!