Swift Playgrounds for iPad and the Learn to Code curriculum are fantastic at introducing students to almost all of the major components of computer science:  abstraction, composition, procedural thinking and computational thinking.  There is one aspect of computer science that is missing from this list, however.  The Apple curriculum delays working with information representation and processing until early in the Learn to Code 2 portion of the curriculum when variables are introduced.  It’s an interesting choice, and we think it’s the right choice.  However, a student’s exposure to information gathering, representation and processing may, as a result, be limited – depending on the depth and breadth of material covered in the classroom.

Archiving, sharing, visualizing and processing user information are one of the primary uses of computers these days and students are keenly aware of these applications.  Many of the most popular applications of mobile devices, including all social networking apps rely on gathering, displaying and sharing information to the user.

Fortunately, as a remedy for more adventurous teachers and students, Apple includes a template called Answers in their Swift Playgrounds application that allows the user to ask for information from the user and display this and other information to the screen.  In this regard, the Answers template acts like a console in a traditional computing sense and is a helpful resource, but it is limited for a few reasons:

  1. There are limited data types that can be requested of a user – only information that fits the Int, Decimal and String types.  While these are the most important data types to support, some instructors may feel limited by the lack of support for Boolean and other more obscure data types.  Additionally, some of the types are misrepresented which can add to student confusion.  There is a function provided called: askForDecimal() which prompts the user for a decimal value.  There is however no Decimal type in Swift, for example.  It would be better if these were referenced by their Swift type names like Double instead of Decimal to avoid inconsistency.
  2. The command to display information to the console in Swift is called print(), but in the Swift Playground Answers template, the function name is show().  Again, this causes confusion for a novice developer as they transition from the Playground environment to XCode or other pro-level tools where the show() command disappears and print() takes its place.
  3. The formatting of the output could be better.  In our opinion, the choice of a proportional-width font vs. a fixed-width (or monospaced) font for the output window may look more modern, but makes the output look bad in some applications.  Applications like an output table will have columns misaligned and simple programs that create interesting ‘ASCII art’ will not look correct in such a window.

We have taken the Answers template and made some slight modifications to it to address the above concerns as well as add some Swift extensions for conveniently generating random numbers in programs.  You and your students can download the Swift Playground Interactions Template in our resources section here:

Swift Playground Book Subscription: Interactions

We are now excited about including some ‘console-based‘ programming in our classes using this template and we think you should consider it in yours for the following reasons:

  1.  It is different.  We have heard a number of students mention that while they love Byte and his clever puzzles that make up the Learn to Code curriculum, they do visually tire of that environment.  Tapping into other templates in the Swift Playgrounds app is just the ticket to spicing up the interest level and providing some diversity from the switches, portals and gems of Byte’s world in the curriculum.
  2. It is familiar.  We have heard a few students using the Learn to Code curriculum discuss that they are not sure how realistic the computer science is they are learning, because Byte’s flashy world, while incredibly engaging to the students, does not look like the computer programming students are accustomed to seeing in other media – the text console with text scrolling from bottom to top.  While as teachers, we much prefer the engaging challenges of the Learn to Code curriculum, there is a lot to gain with supplementing activities in a more traditional environment to 1) provide a break from Byte’s world, and 2) provide some legitimacy to the puzzles they are working on by demonstrating via more traditional console environment that the Swift they write in Byte’s world and the ideas they are learning there are precisely the same as the ideas they need to create a more traditional text-based programs.
  3. Better syntax.  Users can use the print() command instead of show().  Print is the official Swift syntax for printing output to the console.  Using the print() function in Swift playgrounds will make for a smoother adjustment as students later transition into XCode or other professional developer environments.
  4. Console formatting.  We have made the liveView show output from the print command in a format that is more similar to traditional computing consoles.  We are using monospace font which preserves the space for each character.  We are reducing the space between lines to fit about twice as much content in the view.  This makes tables of output align cleanly and allows for the development of simple Swift programs that generate interesting ASCII art.

BEFORE: Output using Answers Template:

AFTER: Output using Interactions Template:

5.  Random number generation.  There are many interesting projects you can do that involve using randomly generated numbers.  Simple games, probability activities and simulations all use randomly generated information.  Unfortunately, Swift’s out-of-the-box functions for creating random numbers are not very intuitive to use and poorly named.  We’re sure this will be fixed in the near future, but for now we have extended many of the data types in Swift to include useful random number generating functions:

let myRanInt = Int.random    // creates a random integer between 1 and 10 and stores it in myRanInt

let myRanDoub = Double.random   // creates a random Double between 0.0 and 1.0 and stores it in myRanDoub

let randomIntInRange = Int.random(from: -20, to: 30)  // creates a random Int in the range -20 ... 30 and stores it in randomIntInRange

let randomDoubInRange = Double.random(from: -5.33, to: 7.14) // random Double in the range -5.33...7.14, stores it in randomDoubInRange

let randomIntInRange = Int.random(range: -7 ... 4) // alternative syntax for creating a random Int in the range -7 ... 4.

We hope you find this new Swift Playground Template a useful and a fun activity for your students.  It is intended to be a springboard to augment your Swift curriculum with realistic console-type applications such as our 1000 Diamonds activity.  We will share more applications with you on Building Rainbows video page as we develop them and we hope you will share yours in the comments section below.

Download Interactions Swift Playground Template


Leave a Reply

Your email address will not be published. Required fields are marked *