Swift iPad Playgrounds:Not Just A Cool Toy

I have problems with the learn to code movement. Apple’s new Swift iPad Playgrounds have brought my problems into focus. There’s a big difference between programming and coding, one that some elements in the learn to code world seem to ignore. When my 12-year-old nephew took a learn to code camp this summer, he learned in Scratch, built a simple game and then forgot about it, and hasn’t coded since. That does not built more and better developers. Scratch is a toy easily abandoned.
On the other hand, early in makeAppPie.com’s existence, I posted one of the first articles online on UITableView and UITableViewController for Swift. I had one enthusiastic teen who, hearing that Swift was easy, built a table view in Interface builder, but could not get it through his head how to use an array to hold his data. He left disillusioned. This is a problem.

Constructivism

For a popular school of thought in the learn to code movement, these two cases illustrate my problems. Languages like Scratch and its predecessor Logo are not production languages. They are limited in their application, and mere toys. Secondly, they fail to contain critical learning points for modern computing, such as arrays or good programming style.
The school of thought behind Scratch and Logo is tied to the educational theory of constructivism, championed in mathematics and computer science by Seymour Papert. Compressing a bigger theory into an essential point, this theory believes that we learn by discovery, and that play is the best way to discovery. Papert was an early champion of using computing for learning mathematics by playing with programming. As admirable and as much as I use play in my own development and learning, there’s a dilemma with play. There are places where completely free play seems scary or is truly dangerous. A shop of power tools or a lab of hazardous chemicals is not the place for a 5-year-old to play. Some tools are easily dangerous, some chemicals are unknown in their use. Some things are just scary. Under supervision older children might handle these situations, but with constraints to their behavior.

The Scary Unknown

To young and old, computer languages present a scary unknown that makes the workshop or lab look friendly. To the uninitiated, statements like a += 10 make no sense. Papert and other Constructivists created languages like Scratch and Swift which took off all the scary shaper edges and left an attractive, graphic rich environment for kids. Removing all the scary parts, removed critical components to a usable computer language. Scratch is like replacing all metal sharp edges in a machine with foam or soft plastic rounded edges. All the functional machines become toys. They might inspire kids to move to real saws, lathes and mills, but do not produce any real objects themselves. While they might entertain or inspire learning, such languages relegate coding to toys.
I’ll emphasize this is not the only school of thought in coding. Though Scratch is on board their system, The Raspberry Pi foundation’s learning core is a modern and powerful production language: Python. So much so, the Pi stands for Python. However, Python programming, like all production languages, suffers from a steep learning curve. Similarly there are learn to code courses based on HTML5/CSS/Javascript. That too is production. Yet, to get the subtleties of Javascript or Python requires class instruction. Self learning, though possible, has a steep learning curve without learning good programming style and important concepts.

Apple’s Agenda

Play is good at a certain point. In a production language, play works after you know some basics. To get those basics direct instruction and demonstration are more efficient than play.  With a basic knowledge, the learner can start to ask the questions that will simulate play. This is true of both an experienced developer and a complete beginner.
All of this Apple considered in their implementation of the Swift playground on the iPad. Apple has a very simple and obvious strategy here: make Swift one of the most popular programming languages in the world. Outside of education, Apple has made some interesting alliances in that direction. In 1984 when I was graduating high school and writing Logo interpreters in Macintosh Pascal, IBM was the Apple’s ultimate enemy. Today they are best buddies, with IBM having a successful web-based Swift sandbox. In May of 2016, Apple and SAP became partners with Swift planned as a development language for SAP’s HANA platform. While nothing has happened yet, there are even grumblings that Google’s Android may eventually adopt Swift . Apple is out to make money, and having the primary computer language world-wide be your language means others  develop software and products that sell Apple products.
In education, play is important. Papert made the assumption there would be a teacher/facilitator available to direct play. In an online world of mobile devices, that assumption is often false. Extreme student to teacher ratios and self-directed learning practically (and tragically) eliminates the teacher. Direction from the app is critical to success to build the basic skills. Apple needed an environment for Swift that direct learners on the path to apps they would like to see in the app store.  A learning platform that looked attractive enough for both kids and adults to play with, but contained the full power of the Swift compiler and a huge number of the frameworks at the user fingertips. It needs to code exactly like it would in Xcode, but does not need a keyboard. Finally, this environment needs an authoring environment to teach the basic skills learners need to play effectively. Their answer is Swift Playgrounds for iPad.

Learn to Code

The playground environment has its own app store of projects called books users can download and use. At the core is the two introductory Learn to Code books.
img_0785
The Fundamentals of Swift assumes no programming experience, and introduces the learner of almost any age to basic Swift programming. Starting with commands, the learner gains knowledge of functions, conditional structures and loops. To end the first course, the learner optimizes an rather sophisticated algorithm for the right-hand maze rule. All the lessons have accompanying puzzle games in the vein of Where’s my Water or  Cut the Rope that follow a collection of creatures on their quest to collect gems.
img_0780
The learner must write the code to get the character to complete the task. The first code is simple: move the character named Byte three spaces forward and collect the gem using the two commands collectGem() and moveForward().

The obvious solution is:

moveForward()
moveForward()
moveForward()
collectGem()

This code runs directly in the Swift interpreter. All classes and public methods the author includes and all Swift commands are available. As Apple mentions several times in the lesson text, any solution works, even ones not yet introduced in the lessons and the Swift language works. For example

while !isOnGem{
    moveForward()
}
collectGem()

Will also solve the same puzzle though while loops are chapters away. The learner is never restricted in their solution. The playground book also emphasizes iterative design, never punishing the learner for a wrong answer. At worst, Byte look a little disappointed, but learners can build their solutions step by step. As the learner goes further into the material, the puzzles get harder, and iterative design almost a necessity. Many of these puzzles will be an entertaining challenge for even veteran programmers. On a nine-hour flight recently, I kept myself well entertained solving these puzzles.
The second half of the Learn to Code series completes the basics covering more complex material, including variables, parameters, types, initialization, using classes, and arrays. By the end of the course, students will be able to code their own worlds and puzzles, or modify ones created for them.
This leads to more playground books available from Apple, all building on this basic knowledge. Learners can customize a Breakout style video game or the game Rock Paper Scissors. They can learn about optimizing search algorithms and testing that code by modifying code for mazes and the game of Battleship.

Keyboard not Required

Swift is a text-based language. While a keyboard is ideal for working with the playgrounds, it is not necessary. The context-sensitive suggestion bar at the bottom of the playground give suggestions for possible code.
image-19
When numeric input is necessary, playgrounds pop up a numeric keyboard
image-20

Control structures and declarations (including class, enum and protocol not shown) come from a pop up menu at the top of the screen, as does graphic assets:
image-21

A keyboard does speed  up  entering codE. Except for initially typing on an identifier, everything can be done without a keyboard.

Beyond Coding

Besides the lessons there are several templates for new code available to the user which allow more free form programming. Three of these, Answers, Shapes and Graphs include classes for the learner to use pre-made objects.
At any time, a user can look under the hood at the code running the playground. The lesson files’ code can be accessed from the tools menu under Advanced. All the source code is in a read only state. More advanced users can read the code and comments in the code as documentation, learning this valuable skill.
Finally the blank template is for completely free form code. The user can use many (but not all) of the frameworks available in iOS including UIKit, SpriteKit, Metal, Scenekit and even Core Data. For a full list you can go here.
With a few lines of code, any developer can prototype using a view controller. A blank playground with the following code:

import UIKit
import PlaygroundSupport
class ViewController:UIViewController{
     override func viewDidLoad() {
         super.viewDidLoad()
         view.backgroundColor = UIColor.blue
     }
}
let viewController = ViewController()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true

Runs a view controller with a blue background. The user adds code to the ViewController class. The user would have to code controls instead of using Interface builder however. After learning, playgrounds become a great prototyping environment for students and developers alike. For example,  I quickly wrote code to make a table view using a table view controller.(you can download that playground here)

img_0807
The iPad playgrounds have one more feature: developers and teachers can author their own playgrounds. Using Xcode, authors can add classes, execute code immediately, and set up chapters and pages in a playground book using a relatively simple file structure and a manifest.plist file. These can be distributed to students or used as templates for development environments.
Apple made the iPad playgrounds part of a larger curriculum. There are videos and curriculum available for extended learning and exercises beyond playgrounds. Apple’s goal is to promote Swift as a programming language and to make the apps submitted in the app store from developers clean, well written code. Apple stresses good problem solving skills, iterative design, efficient algorithms, and debugging as early as possible. The books and videos are well written and designed to help meet this goal. Since Swift shares many attributes of modern computing languages, Swift as a first language is a good step towards Ruby, Python or Java. The problem solving and iterative design themes are valuable lessons for any creative work.
Besides a few bugs in the current release, there’s a few shortcomings to iPad playgrounds. Some form of Interface builder, something like the watchOS version would help in prototyping. I see outside of education, that the biggest use of playgrounds will be the proverbial paper napkin. A fast way of getting controls and coding ideas at a lunch table or at a client’s office will make this a powerful tool for developers.
Swift Playgrounds for iPad’s biggest limitation is in the name. This is an app that requires an iPad running iOS 10 or better. While it works fine on my iPad Pro, I can’t use it on my first generation iPad mini. In short, it only works on expensive, newer iPads. While Apple is distributing iPad to many schools, this still puts Swift out of reach of many low-income families and school districts worldwide. Extending it to iPhones would be nice. A Swift playground that easily runs on a Raspberry Pi is ideal. That of course is a big challenge since the Learn to Code playground books uses Apple’s SceneKit framework.
Apple did something incredible. iPad playgrounds is a full production prototyping tool, called into service as a great tool in learning how not only to code, but to truly program. Apple left the door open for more developers, teachers and authors to write more books for iPad and I’m sure in the coming months and years we’ll see more advanced books on more advanced topics. For a free app, it is one that I’d suggest any developer at any level add to their iPad.

One Reply to “Swift iPad Playgrounds:Not Just A Cool Toy”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s