Schedule‎ > ‎

Tell a Story - Swift playgrounds

Install Xcode if you haven't already. 

Required Reading: 
Review and play with the Balloons playground, and/or the Swift Tour playground. These playgrounds are instructional playgrounds intended to teach Swift, as well as SpriteKit, which is frequently used in developing games. 

WWDC Videos

Today we will be experimenting with Swift through fairytales. Your goal is to choose a fairytale and rewrite the story using Swift. If you need more fairytale variety, check out the Wikipedia page. The goal is to become familiar with Swift Playgrounds and Swift syntax. At the end, we'll leave some time for storytelling. See the TellAStory.pdf for instructions on how to create the playground. 

Here's an example. The Adventures of Pinoccio, as told in Swift. You can also download it in playground format. Challenge: Can you find bugs in my code?

import UIKit

struct Cricket {

    var name : String

    var isSinging : Bool

    var currentlySaying : String? // this is an optional, because the cricket does not need to be saying anything at a given time.

    init() { = "Jiminy" // all crickets start off named Jiminy

        self.isSinging = true // all crickets start off singing



class Character {

    var firstName : String //every character has a name

    var isDead : Bool? //optional because some characters are never real, therefore cannot be dead

    var currentlySaying : String? // this is an optional, because the person does not need to be saying anything at a given time

    init(name : String) {

        self.firstName = name



class Person : Character {

    var isEvil : Bool? // people can be either evil, not evil, or neither.


var jiminy = Cricket() //make a variable called "jiminy" of type "Cricket"

jiminy.currentlySaying = "When youuu wish upon a star..."

jiminy.currentlySaying = "Your dreams come true..."

jiminy.isSinging = false // The song is over now

jiminy.currentlySaying = "There once was a woodworker named Geppetto."

var geppetto = Person(name: "Geppetto")

class Marionette : Character { // Marionette is a subclass of the person class

    var noseLength : Int

    var isReal : Bool

    var isLying : Bool

    var isDonkey : Bool? //optional, because not all Marionettes have the option of turning into donkeys

    func setIsLying (isLying : Bool){

        self.isLying = isLying

        if(self.isLying == true){


        } else {



        self.currentlySaying = "My nose is now: \(self.noseLength) centimeters long!"


    init (noseLength : Int, name: String, isReal: Bool){

        self.noseLength = noseLength

        self.isReal = false //all marionettes start NOT real

        self.isLying = false //no marionette is born lying

        super.init(name: name)



jiminy.currentlySaying = nil //Jiminy Cricket stops talking

geppetto.currentlySaying = "I will name him Pinoccio"

var pinoccio = Marionette(noseLength: 2, name: "Pinoccio", isReal: false)

geppetto.currentlySaying = "I wish Pinoccio was a real boy"

class Fairy : Character {

    var color : UIColor?


var blueFairy = Fairy(name: "Blue Fairy")

blueFairy.color = UIColor.blueColor()

blueFairy.currentlySaying = "If you are brave, truthful, and unselfish, I will make you a real boy!"

jiminy.currentlySaying = "I will watch over him, Blue Fairy!"

blueFairy.currentlySaying = nil //end of conversation

jiminy.currentlySaying = nil

class Cat : Character {

    var isDonkey : Bool? //some cats can turn into donkeys

    let isSneaky : Bool = true //all cats in this story are sneaky and they will always be sneaky


class Fox : Character {

    var isDonkey : Bool? //some foxes can turn into donkeys

    let isHonest : Bool = true //all foxes in this story are honest, and they may never become dishonest


var gideon = Cat(name: "Gideon the Cat")

var john = Fox(name: "Honest John the Fox")

gideon.currentlySaying = String(john.currentlySaying = "Join Stromboli's puppet show, Pinoccio!")

jiminy.currentlySaying = "no, don't do it Pinoccio!"

var stromboli = Person(name: "Stromboli the Puppeteer")

blueFairy.currentlySaying = "Why weren't you at school, Pinoccio?"







pinoccio.currentlySaying = "I will be truthful from now on!"

pinoccio.noseLength = 2 //the Blue Fairy restores Pinoccio's nose to original length.

gideon.currentlySaying = String(john.currentlySaying = "We can convince Pinoccio to go to Pleasure Island and get lots of money!")

jiminy.currentlySaying = "Oh no!"

pinoccio.currentlySaying = "Pleasure Island sounds like fun!"

pinoccio.isDonkey = Bool(gideon.isDonkey = Bool(john.isDonkey = true)); //all three turn into donkeys! 

class Whale : Character {

    var stomachContents : Array<Character>? // whales can optionally have other characters in their stomach, or not.

    func sneeze(){

        self.stomachContents = nil //empties out the contents of the stomach



var monstro = Whale(name: "Monstro, the giant whale")

monstro.stomachContents = Array()

monstro.stomachContents = [geppetto] //Geppetto was swallowed by a giant whale!!


pinoccio.currentlySaying = "Oh no! I must save him"

monstro.stomachContents?.append(pinoccio//Pinoccio is also swallowed by the whale


pinoccio.currentlySaying = "We must make the whale sneeze"



pinoccio.isDead = true

blueFairy.currentlySaying = "Pinoccio has been good! He can be a real boy now!"

pinoccio.isDead = false

pinoccio.isReal = true