Join John Nastos for an in-depth discussion in this video Using Swift playgrounds, part of Learn Swift 2: The Basics.
- When Apple introduced the Swift language, they also introduced an innovative new platform for learning and experimenting with programming called Playgrounds. Playgrounds are an easy way to experiment with a language without the overhead of having to build an entire program. They're a quick way to prototype ideas, especially since they're a live environment where what you type gets compiled and executed as you work. Keep in mind that playgrounds are not meant to be replacements for Xcode projects. They're something else entirely. They have some limitations to be aware of including not being able to run the code you write on an iOS device, no ability to write in other languages than Swift, limited access to user interface controls, et cetera.
Let's jump into Xcode and create a new playground so we can experiment a bit. I'm going to choose Get started with a playground from the splash screen. From here, I can give my playground a name and choose what platform I'm writing for. Keep in mind that the playground won't actually be run on that platform, but this choice does affect which frameworks and libraries the code is linked and run against. Then, I can choose where to save the playground. Once it opens up, it has a couple of lines of boilerplate code to get things started.
The first line is just a comment. That's followed by an import statement which imports the UIKit framework. This is the framework that deals with user interface controls on iOS. Then, there's a variable declaration that holds the string, "Hello, playground." Take a look at the right-hand column, for a moment. This is where some of the magic of playgrounds take place. The right-hand column shows the result of executing that line of code. So, when the variable str is assigned the string "Hello, playground", the result is shown in the right side.
I'm going to write a couple of simple lines of code that actually involve some computation so that you can see a more interesting result. Don't worry too much about the syntax of what I'm writing right now since I haven't gone into describing the Swift language itself. But, if you have past programming experience, you should be able to follow along pretty easily and be able to see the interesting features of playgrounds as I write. First, I'm going to declare a new variable called num, and I'll assign it the value 20. Then, I'm going to write a simple loop that runs 10 times.
Each time the loop runs, I'm going to increase the value of num by 10. Over in the right-hand column, you can see that this line of code gets executed 10 times. If I mouse over this and click on the quick look icon, I can get a graphical representation of the results each time the line runs. If I click the plus icon, Xcode will add the graph in line to my code. Then, when I click on each point in the graph, I can see the value for that iteration.
Now, what happens if I change my code slightly? I could, for example, change my equation so that num gets increased by 10 times I which is my loop index. When I do this, the playground gets immediately re-compiled and re-executed and the graph changes to reflect my edits. Playgrounds are a great tool for experimenting with the Swift language. I often find myself opening a quick playground to test out a few lines of code and see the results clearly, line-by-line, as the program executes.
Because of these great interactive features, the first few sections of this course will all use playgrounds for the code examples.
First, learn how to install Xcode, the IDE designed for Apple developers, and use Swift playgrounds, an innovative coding environment that displays results as you code. Author John Nastos then dives deep into Swift syntax: the variables and constants, data types, arrays, loops, classes, and functions that make your apps run. Next, discover how to build an engaging app interface with storyboards and Interface Builder, connect the interface and code, and use table views to present lists. John shows how to store and retrieve data, and interact with Objective-C, the original app development language that still integrates beautifully with Swift. Using the exercise files provided with the course, you'll assemble a complete working app with Swift while gaining the skills to go create the next one on your own.
- Downloading and installing Xcode
- Using Swift playgrounds
- Understanding Swift syntax
- Debugging Swift code
- Controlling flow with expressions, conditional statements, and loops
- Using and writing Swift functions
- Creating classes, structs, and enums
- Using storyboards in Interface Builder
- Building an app with table views
- Storing and retrieving data
- Interacting with Objective-C