Learning to code isn’t supposed to be this fun. Apple’s new Swift Playgrounds app for iPads running iOS 10 is aimed at teaching the Swift programming language to middle-school-age children and adults, no coding experience necessary. But it feels like a puzzle game—one that just happens to be solved with commands and functions and For loops, instead of with a slingshot full of birds.
And I’m totally hooked. In fact, Swift Playgrounds is my favorite thing to come out of this year’s WWDC by far, and I think it’s going to be one of the highlights of iOS 10 when it’s released this fall.
You can try out the free Swift Playgrounds app today by putting the iOS 10 public beta on a compatible iPad (fourth-generation or later). The app is automatically added to your home screen when you join the beta, but when iOS 10 launches this fall, Swift Playgrounds will be a free download from the App Store.
When you fire it up, you’ll see a Featured section with lesson packs named Learn to Code 1: Fundamentals of Swift, and then Learn to Code 2: Beyond the Basics. Below that are the Challenges, starting with Drawing Sounds and Blink. More lesson packs and challenges will be added over time, and the lessons are RSS-based, so Apple can add new content without having to update the app itself.
Downloading a lesson pack or challenge adds it to the My Playgrounds tab, which looks like the shelf in iBooks. You can create new playgrounds too, or download them from other sources. For example, Apple made a Sphero playground to demo at WWDC, showing how Swift Playgrounds can use all of the iPad’s hardware and software features, in this case connecting via Bluetooth and issuing commands to a Sphero robotic ball. Sphero could actually make its own playground and distribute it on its own site, explained Wiley Hodges, Apple’s director of tools and technologies product marketing.
From this shelf, you can also duplicate a playground or reset it. Say your child is halfway through a lesson her little sister wants to try, too. You can duplicate the playground to make a copy, then reset the copy to its original state. (Swift Playgrounds will also work with the classroom-management features Apple added for teachers in iOS 9.3.)
Learning to code
Since I had no Swift experience, I plunged right in to Learn to Code 1, which assumes no prior knowledge. A friendly cut-scene introduces each new topic and explains it in a way anyone can understand. The first group of lessons, for example, covers basic commands, so the cut-scene makes the analogy that these are like following a recipe or set of instructions. Later, when introducing For loops, the analogy is breaking down a task like planting a flower garden into small steps you’ll repeat over and over.
But once the concepts are introduced, the lessons use the real terms for everything: call a function, loop commands, if statement, conditional code. Anything jargony in the instructions is colored red, and all you have to do is tap a red word for a definition.
The interface is split into two panels. The left side is text: Your challenge is explained at the top, with a space below for you to compose your program. The right side of the screen shows the results, with a big red Play button you can tap, labeled Run My Code. For the Learn to Code 1 lesson, I’m giving commands to Byte, a goofy cartoonish mascot who’s moving around a game board collecting gems and flipping switches. (Easter egg: Tap on Byte to change the character model; there are three to choose from.)
You’re given some commands to get started: You can tell Byte, for example to moveForward and turnLeft, which are both examples of how Swift commands really appear. But there’s no turnRight command out of the gate, so one of my first challenges was writing a turnRight function that combined three turnLeft commands. You can run the program after entering each command, or try to write the whole thing out at once by tapping the commands in the QuickType row or expanding the whole keyboard. The preview pane supports Multi-Touch, so I was able to scroll around the game world and zoom in and out to count, for example, how many squares Byte needed to move forward before he turned left.
Learn to Code 2, which will launch with the final version of Swift Playgrounds this fall, has a different world than the gem-collecting, switch-flipping realm of Byte, which is meant to show that Swift’s tools can be used for tons of different applications, as well as to cut down on repetition.
Thinking like a coder
Swift Playgrounds encourages creative problem-solving—there isn’t one single solution, or even one “best” solution. You aren’t graded on how few lines you can use or how many functions you come up with. But the app does teach good coding habits like looking for patterns that repeat, and using descriptive names for functions so you can more easily remember what they do. If you break out ahead, say, adding a loop to your code before you are formally taught loops, the app gives you props for your advanced skills.
The whole experience is heavy on praise, which made me want to keep going lesson after lesson—it’s really motivating! And a progressive Hint button cuts down the frustration factor if you do hit a wall. At first, the Hint popup offers ways to think about the challenge. If you need more help, it will supply clues about how to break down the challenge into parts, and finally just shows you the correct code if you’re really stumped. But you can’t copy and paste from the Hint bubble into the code editor—you have to type it out manually, which will help you learn.
It’s a lot of fun to tinker Swift Playgrounds, learning the concepts behind making good code as well as the syntax, but your code isn’t trapped there. You can record and share movies of your code, even give them voiceovers bragging about your skills, or even share entire playgrounds via AirDrop, Messages, and Mail. The iPad isn’t a full development platform just yet (and naturally, Apple wouldn’t discuss its plans for the future), but you can also export code from your playgrounds back into Xcode on the Mac.
Why it’s so unique
Apple introduced Swift at WWDC in 2014, and made it open source this past December. It’s already being used in more than 100,000 apps in the App Store, including heavy-hitters like Lyft, Slack, and Strava. “We’ve gotten a great response,” said Hodges, adding that Swift is the fastest-growing programming language.
The benefits of Swift is that it’s fast and powerful like compiled languages like Objective-C, while also being more friendly and easy to read like a scriping language. Commands and functions are built with human-readable words. My first encounter with writing code was back in the ’80s, when I learned BASIC at a summer day camp for nerdy children, and I loved how I could tell what it was going to do before I actually ran it, just by reading the code. Swift reminds me of that, and Swift Playgrounds’ splitscreen view makes that visualization even easier, the code listed on the left, next to the virtual world where it’ll run.
In fact, Swift Playgrounds is (naturally) written in Swift, and the code you create in the code editor is inserted directly into the program running on the right side of the screen. Once I accidentally deleted a bracket and broke the code. It wasn’t a problem of the onscreen Byte character not reaching his goal because I’d given him the wrong commands and sent him in the wrong direction. He wouldn’t move at all until I fixed the syntax, an in-my-face reminder that every keystroke matters. The game world uses SceneKit, Metal, and 3D acceleration, the same technologies used all over iOS.
Apple developed the curriculum for Swift Playgrounds itself, based on what its own software engineers think is the best way to learn software engineering. This isn’t a purely academic exercise: Swift Playgrounds is designed to get you thinking like a coder, decomposing problems and applying logic. “It’s made by Apple, by the people who write the real software,” said Tim Triemstra, who handles product marketing for Apple’s developer tools, “so we’re teaching programming how it’s actually done.”
But Apple also brought in professional educators, and of course kid testers, to tweak things like the leveling and the motivating language. “We worked with educators to hit the important notes of how to teach computer science,” explained Cheryl Thomas, Apple’s vice president of software engineering operations. “We wanted to be egalitarian, with a wide appeal to both boys and girls,” she said.
All the lessons are available right away, so you don’t have to unlock them one by one or play them in a certain order. The goal was to create excitement, so a lot of thinking went into how to keep users engaged—and it really shows. Swift Playgrounds feels more like a game than an academic exercise, with a “just one more lesson” addictiveness that made me want to keep building on the skills I’d just learned.
Swift Playgrounds is available with the iOS 10 public beta, including Learn to Code 1 and two Challenges. More lessons will come later with the final shipping version of the app, launching with iOS 10 this fall. For more on Swift Playgrounds, take a listen to Macworld Podcast episode 514, with special guest Fraser Speirs.