This week's sponsor


Business Productivity App for Mac & iOS

Swift Coding Comes to iPad: Playgrounds, Schools, and Learning to Code

Right at the end of the WWDC 2016 keynote, Apple announced Swift Playgrounds. This is a new app from Apple that is designed to allow children to learn to program on an iPad. This is a first from Apple and a major advance for iOS as a platform.

I was fortunate to be awarded an educator scholarship to WWDC 2016 and was privileged to be in the audience at the announcement. While attending the conference, I was able to speak with many of the engineers and educators working on Swift Playgrounds and gain an insight into what the software is capable of and the reasons why it was built.

What is Swift?

Swift is Apple's own programming language. It was introduced at WWDC 2014 and has been expanded and developed ever since. Historically Apple – and before Apple, NeXT – used a programming language called Objective-C. This was a language that extended the C programming language to include a range of features that made it easier to program large-scale modern applications.

Objective-C was getting quite old by the early 2010s. Apple began a project to create a replacement language that included many of the modern advances in language design that had been developed in the period after Objective-C was first defined in the early 1980s. The result is Swift.

Today, Swift is a full peer of Objective-C in Apple's developer ecosystem. Although Objective-C has not been abandoned, the clear direction of travel at Apple is towards Swift and away from Objective-C. At WWDC 2016, for example, all code examples shown were in Swift. The Playgrounds concept that we will discuss in this article has only ever existed for Swift.

Programming Education on iOS

I have been a Computer Science teacher for ten years. I started teaching children on Mac desktops in 2006 and we used a variant of Visual Basic for Macs called Real Basic. Later we moved to teaching Ruby using Terminal and Bare Bones' basic text editor TextWrangler.

When our school went 1:1 iPad in 2010, we continued using the Macs for programming education for a few years. In 2012, we made our first move towards teaching programming on iOS. This approach involved running an Amazon EC2 Linux server in the cloud and connecting to it from the iPad using apps such as Textastic and Prompt. We continued teaching Ruby this way and it was reasonably successful, if a little complex to understand. Regardless, I was able to teach up to pre-university exam level using this system.

As time went on, a number of other options arrived for programming directly on the iPad itself. At an introductory level, apps like Hopscotch brought the visual block-based programming style popularised by MIT's Scratch to iOS. Another app, Codea, is focused on game programming with the Lua language. The ne plus ultra of iOS programming tools, however, is Pythonista.

In 2015, we rewrote our computer programming course to use the Python language and adopted Pythonista as our programming environment. While far simpler to deploy and use than the old EC2 system, Pythonista is limited by some of Apple's App Store policies about downloading and running code on iOS. It remains the most full-featured native development tool on iOS and one of the most impressive applications on the platform.

Everyone Can Code

Everyone Can Code is Apple's initiative to promote Computer Science education in schools and beyond. The initiative, announced at WWDC 2016, has a number of strands. The first and largest strand is the creation of the Swift Playgrounds app, which we will discuss later in this article.

Closely related to Swift Playgrounds is the production of two interactive textbooks called Learn to Code 1 and 2. These books are essentially a full curriculum for learning the basics of Swift.

The third strand in Everyone Can Code is a range of iBooks for teachers and students that support the Learn to Code curriculum and extend the learning into working with Xcode on macOS.

The first book is the Swift Playgrounds Teacher Guide which supports teachers who are often charged with delivering 'coding' in their school in line with political initiatives, regardless of their actual ability or experience in doing so.

It's important to understand the context in which the current push for 'coding in schools' is happening. Very few teachers in the elementary stages are able to program at all and many high schools have between zero and one Computer Science teacher. Apple's curriculum initiatives are important because there simply are not enough capable teachers in the world to give "everyone" an opportunity to "code".

The other two books in the Everyone Can Code series are App Development with Swift and a companion Teacher Guide for that title. App Development with Swift is designed to bridge the gap between Swift Playgrounds on iOS and writing a full app with Xcode on macOS.

Swift Playgrounds

Swift Playgrounds is the flagship of Apple's Everyone Can Code initiative. It is an iOS app that runs on iPads running iOS 10 and provides a native Swift programming environment on iOS.

Swift Playgrounds have been a feature of Xcode on macOS for some time. Playgrounds are files of code that can be used to experiment with various programming structures outside of a full Xcode application project. Swift Playgrounds for iOS allows you to transfer these files from Xcode and run them on iOS. You can also write directly into a blank Playground on iOS.

The UI of Swift Playgrounds is split into two halves. The left hand side is where you code; the right hand side contains the Live View – an area where the programmer can place views and other kinds of output.

Playgrounds also contains a number of innovative features in the text editor to make writing the code easier on a touch screen.

Firstly, individual code statements can be dragged and dropped without having to first select them in the editor. Where a programmer wants to use what is called a literal value in the code, they can enter that directly. Most programming editors allow you to type in literal numbers, text and true/false values. Playgrounds extends this to allow the programmer to pick literal numbers from a pop-up number keypad, literal colour values from a colour wheel and even literal images from the standard iOS photo picker. This will make creating visually engaging code much easier for learners.

In Swift code that contains looping or conditional structures, that code is surrounded by curly braces. It's often a source of frustration for beginning programmers to have to manage what code is inside and outside of the braces. Swift Playgrounds allows the programmer to simply drag the closing brace around to include or exclude statements from the loop or conditional.

Suggestions and error messages in Playgrounds.

Suggestions and error messages in Playgrounds.

Finally, Swift Playgrounds has a new take on the autocomplete suggestions toolbar that appears at the top of the iOS keyboard. When programming, the autocomplete bar will suggest function and variable names that the programmer might want to use. These suggestions are context-sensitive, so it doesn't suggest things that would be invalid to enter. The suggestions also pick up variables and function names that the programmer has already entered, speeding up the programming process a bit more.

At first glance Swift Playgrounds looks a bit like a toy, but appearances are deceiving here. Swift Playgrounds provides full access to almost all parts of the iOS APIs that programmers use to develop full iOS applications. You can't actually develop an iOS application in Swift Playgrounds, though. The system is designed for learning how to program first and for exploring and learning the iOS APIs.

Opening a web view in Playgrounds.

Opening a web view in Playgrounds.

Some of those APIs are very powerful. Programmers have access to views like WebKit – the view that powers Safari. It's possible to build a (very basic) web browser right inside Swift Playgrounds. Playgrounds allows access to most of the iOS APIs, including things like SceneKit, CoreLocation and others. The only major API I found that didn't work was MapKit.

Learn to Code 1 and 2

Swift Playgrounds introduces a new kind of interactive textbook to go alongside the Swift Playground file that Xcode introduced a few years ago.

A "Swift Playground Book" is a document that allows the author to create multiple pages of content interspersed with working program code that the learner can edit and run. The first two Swift Playground Books are Learn to Code 1 and 2, and they are very well done.

Learn to Code 1 is an introductory book aimed at children 9-11 years old. It teaches the basic elements of what we call Computational Thinking. Computational Thinking embodies many of the skills that programmers use to solve problems with computers.

In Learn to Code 1, the learner works through a series of problems by guiding an alien called Byte through a series of fantasy puzzle worlds. These worlds are tile-based and include interactive elements such as gems, switches and portals that Byte can interact with if the learner uses the appropriate commands.

At this level, Learn to Code 1 is really all about problem solving. It introduces a number of Computer Science concepts including functions, conditionals, loops and general algorithm creation. Learners can access hints and tips as well as a provided solution for the problem if they get really stuck.

Learn to Code 2 extends this by introducing more advanced concepts such as arrays and a few concepts from object-oriented programming. It also has exercises that allow the learner to extend and manipulate the puzzle world itself. Another interesting difference is that Learn to Code 2 provides "challenge" puzzles where there is no provided solution and the learner has to work it out entirely for themselves.

Both books are beautifully produced. My first thought when I saw Byte's puzzle world was "oh dear, my exercises won't look as good as that!". I believe children will find the books engaging and motivational. As a teacher, I appreciate the care that has been taken to write the content and to not make it too easy for learners. I can easily imagine some of the puzzles taking one or two hours for a beginning programmer to complete.

The format of these books has been documented by Apple and is open for others to create similar titles. The process is not particularly easy right now. There is no dedicated authoring tool except Xcode on macOS and the structure of the book is essentially a set of deeply nested folders containing Swift code and resources. It's not particularly difficult to understand but I don't envisage every teacher rushing to make their own Playground books just yet.

Should We Be Teaching Swift in Schools?

A perennial debate in Computer Science education is which languages should be used for teaching?

Some argue that teaching industry-standard languages is the most appropriate approach. It prepares students for the "real world" and gives them readily marketable skills. This view is perhaps more common in post-school education where learners will shortly be entering the job market.

On the other hand, school teachers will often tell you that the most important thing is to use a good teaching language, regardless of its relevance to the current job market.

In my own Computer Science education, I learned and used many languages in the process of going from playing with programming as a five-year-old to a Masters in Computer Science. I started with Sinclair Basic on the ZX Spectrum in 1984. I learned Pascal and Hypertalk in school. Next, I moved to Ada 95 in first-year University. From there, I moved on to Haskell, C, and Java. In my first job, I learned PHP, Perl and Objective-C. When I started teaching, I learned Visual Basic, then Ruby, then Python and now Swift. That's fourteen programming languages over the course of about thirty-five years.

The point I'm making is that it's a fundamental error to believe that the only programming languages that can be learned are the ones that are formally taught in schools. My personal view is that the best programming languages for teaching are the ones that allow learners to learn the concepts easily. If a learner understands the concepts, learning a new programming language is the work of a few days.

Apple is in a fortunate position in that Swift is both a good programming language to teach with and an industry-standard language. It runs on a wildly popular mobile operating system that many students will be familiar with.

Some of Apple's Everyone Can Code marketing has focused a little too much on the supposition that "Kids love apps. Apps are written in Swift. Therefore kids will love Swift". I believe there are good Computer Science education reasons to use Swift, and I would like to see these arguments come to the fore a little more.

That said, I completely understand why Apple is deploying that first line of argument to schools. Almost nobody who is currently employed in curriculum coordination and school development planning would understand the argument for teaching a strongly-typed language with consistent semantics and modern syntax.

A final argument you might hear is that schools shouldn't teach proprietary languages. That's a sentiment similar to the notion that schools should give pupils experience of a range of computing platforms in school.

These are laudable aims that, curiously, nobody ever cared about until it was Apple's platform and Apple's programming language that threatened to dominate. How many schools ran Linux or Mac OS X in the 2000s? How many schools taught any open-source programming language alongside Visual Basic?

Next Steps

Swift Playgrounds runs on any 64-bit iPad that supports iOS 10. That's not the same thing as every iPad that runs iOS 10. Swift Playgrounds requires an iPad Air, iPad mini 2, or later. The only people left out of this are owners of the 4th Generation iPad, which is the last 32-bit iPad supported by iOS 10.

As mentioned in the iPhone 7 keynote, many schools are already committed to using Swift Playgrounds in their Computer Science education. My school will be among those doing so later this autumn.

I'm personally very excited to get started using Swift Playgrounds in school. I'm even more excited to see what the app develops into over the coming years.

Unlock MacStories Extras

Club MacStories offers exclusive access to extra MacStories content, delivered every week; it’s also a way to support us directly.

Club MacStories will help you discover the best apps for your devices and get the most out of your iPhone, iPad, and Mac. Plus, it’s made in Italy.

Starting at $5/month, with an annual option available. Join the Club.

A Club MacStories membership includes:

  • MacStories Weekly newsletter, delivered every week on Friday with app collections, tips, iOS workflows, and more;
  • Monthly Log newsletter, delivered once every month with behind-the-scenes stories, app notes, personal journals, and more;
  • Access to occasional giveaways, discounts, and free downloads.