Join Scott Gardner for an in-depth discussion in this video Use iPad playgrounds, part of Swift 3 Essential Training: Beyond the Basics.
- [Narrator] If there's an even more fun way to play around with Swift code, especially when you're on the go, it's by using Swift Playgrounds for iPad, which is a free download from the app store. I'm showing my iPad screen now and I'll tap the Playgrounds app to launch it. Now I'll tap the big plus button to create a new playground. And I'll just select the Blank Template. I'll tap into the file to enter some code. And I'm using an external keyboard. But notice at the bottom, that there's a toolbar with code snippet tips. I'll create a constant called Hello World by tapping let, and this inserts a snippet with placeholders I can tap on to give my constant a name and a value.
I'll name it Hello World and then I'll tap the value placeholder and rather than typing the quotes to assign a string value, I'll tap the quotes ABC snippet. This inserts a string and highlights the string inside the quotes, ready for me to replace it with my own text. I'll type Hello World. Now I'll enter a couple of line breaks. And I'll write a print statement. Notice that as soon as I start writing Print, useful suggestions are presented.
I'll select the first one. And now my constant name is suggested. So I'll tap it. I'll tap the Run My Code button in the bottom right hand corner and the output will be displayable in a pop up by tapping the icon to the right of the code. In the pop-up, you'll see that you can also add a viewer, which will add the output to the Playground editor, similar to clicking the Show Result button in the Results sidebar in an Xcode playground on the Mac. The code snippet suggestions are a real time saver but for more complicated blocks of code, you can tap the plus button in the top right hand corner.
I'll select a four loop. And now I can either manually enter a number for the end value or else use the slider. I'll use the slider to enter a value of 10 just because it's more fun than typing it out. And I'll just output I in the body. I'll execute my code and tapping the result shows a graph. And I'll add a viewer for it. I could type additional code in the body of the four loop, but instead I'll just drag and drop my print statement in there.
A few more things I want to point out. You can use the Undo and Redo buttons in the toolbar to well, undo or redo your work. You can move the cursor around by tapping where you want to place it or by tapping and holding to drag it around. Just remember to tap Run My Code when you want to see the results of the changes you've made. There's of course, a ton more you can do with Swift Playgrounds for iPad. If you're inclined, tap the top left button to return to your Install Playgrounds, then tap the Feature tab.
Here you can browse and download playgrounds from Apple. And guess what? You can transfer playgrounds you created in Xcode to use on iPad. To transfer a playground, you can email it to yourself but an even easier way is to use AirDrop. I'll open the Exercise files folder on the Desktop and drag the Playground file I want to send to my iPad to AirDop and drop it onto my iPad once it shows up.
After a few seconds, the Playground is now available to use on my iPad. This includes imported frameworks and files added to the Sources and Resources folders. For the most part, everything works just as it does in Xcode on the Mac. The links to the additional playground pages also work. But if there's something that doesn't work on iPad, they'll be a red dot next to that code to indicate an error. Tap it to reveal the error. In this case, Playground Shared Data Directory is not available in iPad, which makes sense because it references a folder on my Mac, which is not on the iPad, although I wouldn't be surprised if a future feature enables broadcasting that shared folder to iPads on the same network, which would be quite useful in classroom settings.
But still, most everything you can do in a playground in Xcode, is available on iPad. I'll tap the next link to go to the next page, then tap Run My Code and I'll tap the result for the print statement. To see a list of all frameworks supported in iPad playgrounds, check out Apple's Playground Book Format Reference, Available Frameworks page.
- Adding source files, resources, links, and literals
- Adding pages to a playground
- Using overflow operators and bitwise operators
- Using ranges with strings
- Creating complex sequences
- Chaining higher-order functions
- Defining lazy properties
- Using failable initializers
- Mutating methods
- Working with singletons
- Nesting function types
- Creating error types and recursive enumerations
- Extending concrete types
- Referencing selectors and key paths
- Working with protocol-oriented programming
- Defining class-only protocols and optional protocols
- Using option sets, type checking, and casting operators