Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 1: Core Concepts

02. Swift Playgrounds & Comments

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Introduction Next episode: 03. Booleans & Comparison Operators

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 02. Swift Playgrounds & Comments

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

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

So, if you finish building the Bullseye game in your first iOS app, then you're already familiar with using Swift and Xcode's standard editor, but Xcode has another interface called playgrounds that you'll be using in this course. So let's get familiar with how they work. Using playgrounds is a great way to learn the Swift programming language. You don't have to create a whole project in Xcode. You just have to write little bits of code. So they're really lightweight. 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 with Swift. This way you can easily focus on the core concepts of the Swift language, like functions and classes, without getting bogged down and worrying about UI like buttons and labels and sliders. To create a new playground in Xcode go to File, New, Playground. go to File, New, Playground. There's also a keyboard shortcut, option shift command N. There are templates for playgrounds just like for full Xcode projects. Now I want a blank one. Name your playground. (keyboard clicking) 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 opens up in Xcode. Okay, now we're ready to continue with a tour of Xcode. The main area here is called the source editor. And 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 Bullseye. There are a few differences. If you started with a blank template, you only have one file in your playground instead of the multiple files you get in an Xcode project. Now 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 result sidebar on the right. This is what makes playgrounds really special, and it sets them apart from Xcode's standard code editor. Playgrounds can run your code constantly as you type 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 it that this variable str has been successfully declared and set to a value of, "Hello, playground." Now, I don't really like using abbreviations for variable names. So I'm going to change this str to welcomeMessage. (keyboard clicking) (keyboard clicking) Now 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 also choose to switch between automatic execution and manual execution modes if you hold down this button. Now, 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 it's ready to handle more code in the source editor. When the playground is actively executing, the viewer will display a spinner, and you can see that for yourself if you type some code into the editor. So 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 goodbye message and set it to the value of, "See you soon!" (keyboard clicking) (keyboard clicking) (keyboard clicking) And you'll see that spinner in action. And there we go. Now you can 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. But that's not quite the case though. Let me print out goodbyeMessage to show you. (keyboard clicking) (keyboard clicking) Oh, 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 anytime you'd like. There's a button to toggle the console in the upper right here, or you can use a keyboard shortcut, shift command Y. So you can think of playgrounds like a scratch pad for trying out Swift code. I use it to test 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. Maybe you want to add a note to remind yourself to do something later, or maybe you just want to put some dividers in there to help organize your code. Now, comments are exactly that. That's text. That's ignored by the compiler. So let's take a look at how to add comments in Swift. To add a comment, it's as simple as typing two forward slashes, (keyboard clicking) (keyboard clicking) and then type whatever comment you want to add. (keyboard clicking) (keyboard clicking) (keyboard clicking) You can tell that it isn't executed because your playground is already compiled. You can still see everything prints out. So you know the playground is running as expected, and you can just keep adding lines and lines of comments like this if you want. (keyboard clicking) (keyboard clicking) But if you run comments that span multiple lines, there's an easier way. Start with a forward slash asterisk. (keyboard clicking) Add your comment. (keyboard clicking) (keyboard clicking) (keyboard clicking) (keyboard clicking) And finish it off with an 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 you don't want to delete it or lose it. Let's say you don't necessarily want to print out this goodbyeMessage variable anymore, but you want to keep this line of code around temporarily for some use in the future. So just move your cursor to the front of this line of code and type two slashes. This is known as commenting out your code. This comments out the entire line, and you just see the result dim or disappear in the sidebar and the console as well. 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. And if you change your mind, you can use the same shortcut, command forward slash, to uncomment the code. Now, if you comment out code, you should only do so temporarily. So remember to delete any unused or unnecessary code from your projects. Commented out code that's left hanging around in your projects is only going to confuse you in the future as well as anyone else working on the project with you. Now, there's one last thing I want to show you about comments, and that's a special feature of playgrounds that lets you put special markup code that looks and works differently from the rest of your Swift code. So you may have wondered about these previous and next sections at the top and the 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. It lets you format text and add things like links. Now you don't need to know Markdown for this course, and I don't expect you to create any comments like this. But I figured you'd like to know how those things got to the top and the bottom of the playgrounds. Select Editor if you rendered markup to get back to the normal view of your playground. Now 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.