See how Playgrounds can help you learn Swift. Configure Xcode settings for this course. Explore basic Swift syntax and learn how to graph numeric results.
- [Instructor] Playgrounds are an interactive sandbox environment that enables you to learn and explore Swift and visualize the results of your code in real-time after each edit and without requiring you to manually build and run. I'm going to use playgrounds throughout the course, so I'll show you how to create one now. Launch Xcode if it's not already running, and then either click get started with a playground in the welcome window or select file, new playground from the menu. Accept the suggested file name or give it a new name, and select iOS for the platform.
Platform selection determines which framework is imported into the playground. We'll mostly be using the Swift standard library and foundation framework throughout the course, but I'll also be using code from the UI kit framework. So be sure to select the iOS platform. And click next, and then click create to save the playground someplace convenient, such as your desktop. I'll go ahead and maximize the window. This is your playground window. There's lots to explore and have fun with in playgrounds, but I'll stay focused on some of the key components of playgrounds that I'll be using in this course.
The source editor is where you type your code. If you don't see line numbers in the gutter along the left side of the source editor, select Xcode preferences from the menu, click the text editing tab, and check to show line numbers. This is optional, but it makes it easier to reference specific lines of code. To the right of the source editor is the results sidebar. It displays the results of each expression as you type it into the editor. I'll come back to that in a moment. First, I'll show the debug area by clicking the show hide debug area button in the bottom left corner, below the source editor.
The debug area will display error messages and the results of print statements. In the toolbar at the top, the status displays ready. As you edit your source code, the status display will change to running whatever you named your playground file. This is automatic usually. If necessary, you can stop executing or tell the playground to reexecute your code by clicking the execute stop playground button in the bottom left-hand corner, next to the debug area button. A new playground starts out with three lines of code. First is a comment defining the word playground.
Notice the double slashes are followed by a colon. That's markup. Playgrounds support markup to allow you to create rich, in-line documentation in a playground. I'll go over using markup in my adding comments video later in this chapter, but with markup, you can toggle displaying it as raw code or rendered markup by selecting editor, show rendered, or raw markup from the menu. Next is an import of UI kit. It's necessary to import an external framework before use. Remember that I chose iOS for the platform a moment ago.
This caused the UI kit framework to be imported here. If I try to import app kit at this point, which is a framework used for Mac OS app development, I'll get a red dot with an exclamation mark in the left gutter, and see an error in the console reporting the error. No such module app kit. Clicking the red dot will also display the error. I'll go ahead and undo that change. In other situations though, you may see red dots with white dots in them, or yellow warning icons in the gutter, representing errors and warnings respectively. Clicking on those icons will typically display helpful explanations and fix it suggestions.
The last line of code, var str equals hello playground, is a simple definition of a string variable. This line of code begins with a variable definition. I'll get into variables and strings in later chapters. For now, there's a few things worth mentioning. First, check out the results sidebar on the right, which shows the value of the str variable. Only print statements and errors will be displayed in the console at the bottom, but all values will generally display in the results sidebar adjacent to the line they are defined on in the source editor. Also notice that the line of code does not end with a semicolon.
Semicolons are optional in Swift and generally not used. The only time semicolons are necessary is if you want to write two or more statements on the same line. For example, I'll define hello and playground variables on a single line. After the first variable definition, to let the compiler know that this is a complete statement. Okay, I'll do something a bit more interesting now. I'm going to write some code that I haven't covered yet. I'll only briefly explain what's going on here now, but I'll go over it in detail in later chapters. First, I'll create a for loop that iterates 64 times.
The zero dot dot left angle bracket 64 represents a range of zero to 64, and I is an integer representing each value in the range as I iterate through it. Whatever I write in the body of this for loop enclosed in the curly braces will be executed 64 times. So in the for loop, I'll define a variable point equal to the result of calling the sine function, passing to it the current value of I as a double, because the sine function works with doubles, not integers. And I'll multiply that result by 100.
Notice the results sidebar displays 64 times. This will create a series of 64 values that, if plotted, would display a sine wave. Hovering over the results sidebar displays two icons, quick look and show result. Clicking quick look will display a graph and a pop-up, and clicking the show result will insert the graph into the source editor. Right clicking on the graph, I'll get options to switch between displaying the latest value, a scrollable list of the value history, or the graph. This barely scratches the surface of what playgrounds can do.
Really, a whole course could be devoted just to playgrounds. But I've covered enough to move on. Next, I'll go over how to print to the console using the same playground.
Learn how to write code, understand Swift's key concepts and best practices, and strengthen your programming problem-solving skills. Instructor Scott Gardner teaches the fundamentals, so you'll be prepared to develop applications for iOS, macOS, and other platforms. Completing this course will enable you to not only write first-class code, but to think like a Swift developer.
- Creating playgrounds
- Defining variables and constants
- Working with characters and strings
- Working with collections and groups
- Using operators and defining custom operators
- Controlling program flow
- Defining functions and closures
- Working with classes, structures, and enumerations
- Adopting protocols