Learn how to edit code in a playground. Remove syntax errors. Add code for executing the class. Run the playground. Use a quick look and step speed to display the results of the playground.
- [Instructor] You've created a class in playgrounds. If you haven't yet, you can download a copy from the examples folder. Now you want to run it. But before we do, there's a few things I want to bring up about errors. First of all, I'm not happy with that identifier price. For arrays, I like to have them in the plural so I can identify collection types easily when reading code. Tap right after the word price or on right on the word price and change it to prices. When you do, you'll see a red dot appear. That's a syntax error. Tap the error, and you get a pop up for the error.
In this case, playgrounds gives you a suggestion on how to fix the error that price in the print statement is wrong and really should be prices. So go ahead and hit fix, cause that's correct. And it fixes the prices. I also have another error here, which I'm just going to quickly fix and that's at the pizza list is a small p not a capital P. Classes should have a capital P. So, I'll go ahead and hit shift, P I Z Z A, shift list.
So I've gone ahead and fixed that one. Now, I can add some more code underneath that. Playgrounds run code directly in the playground. After you define a class, you use it directly on the code page. Add two lines after your class. Let pizza list equal the class pizza list. And, we'll have a blank constructor. And then after that, pizza list dot list.
You're ready to run. Tap run my code. Not much happens except for an index out of range. If you look at the code real quickly, you'll see that we've made a mistake. The index we used was I. And I really need to use I minus one, which is my index value that I had just made. So, I'm going to change those I's to indexes. I'm going to go ahead and run my code. And it seems to work, though not a lot has happened.
You'll see a few new symbols on the right side of the playground. Those are quick looks. Hit the second of the seven x's. And you'll see Pizza Sweet Home Chicago at $22.99. Quick looks tell you the state of this line. Tap add viewer. And we'll place that under the line so you can read it. That's your output. The seven x tells you this line ran seven times. iPad playgrounds do not print out to the console. You have to use quick looks or other output.
We'll discuss later how to make the other output show on the live view. Hit the speed gauge. You can see you can step through your code or run your code. Tap step through my code. The code runs with a pointer showing what statement it runs. As it hits the print statement, the quick look changes listing each of the pizzas and price. You have options on how to run your playgrounds; fast, step, or step slowly.
Letting you watch the flow of your application flow and using quick looks to see how the code performs. As we get deeper into playgrounds, we'll discuss quick looks and their role in debugging a lot more.
- Comparing iPad and Xcode playgrounds
- Creating and testing Swift classes and code in iPad and Xcode playgrounds
- Prototyping code
- Debugging and testing with Quick Look live views
- Using markup to interactively document code and create educational lessons
- Compiling code into playground books