Your paper prototype shows what your app looks like, and your clients have now tried it. Now you plan what it holds and what it does, before you write any code.

In Building Multiplication Maestro you built a list and a procedure that I planned. Here you plan your own. keepResults(matching:) and its results list have the shape College Board asks for, but your list and procedure must be your own idea, for your own client.

You plan this on your own, by hand, on paper. Later, on the AP Exam, you answer written questions about your list and your procedure on your own, so plan something you understand completely.

Try it first

Look at your paper prototype. Put a tick on every card where the app has to show or use something that there is more than one of: questions, animals, words, levels, pictures, sounds.

Now look at the ticks. What would it take to add five more of those things later? If the honest answer is “copy and paste a lot of code”, you have just found where your list belongs.

Plan your list

College Board asks for a list that stores a collection of data your app uses, and that makes your program easier to write, or easier to change, than it would be without the list.

On paper, write:

  • Its name. Name it after what it holds: animalFacts, not myArray.
  • What one item holds. If one item needs more than one piece of information, list the pieces and their types (for example, a name that is a String and a length that is an Int).
  • Three example items, written out in full.
  • Why a list? One or two sentences: how does the list make your app easier to build, or easier to change later, than it would be without one?

Plan your procedure

Your procedure does something with your list that your app needs, and what it does depends on a parameter. For example: choose the questions for a level, count how many answers were right, pick the animals that match what the child chose.

Check your idea against College Board’s requirements. Your procedure:

A button’s action or a view’s body does not count as your procedure: College Board does not consider built-in structures like these to be student-developed. Your procedure is a function you write yourself and call.

Then write its steps in plain language, numbered, the way you would explain them to a classmate.

Choose your test inputs

A procedure that runs is not the same as a procedure that is right. Before you write yours, try this with the person beside you, at a whiteboard.

A spelling game should show every word shorter than the length the child picks. Here is the procedure:

func keepWords(shorterThan maximumLength: Int, from wordList: [String]) -> [String] {
    var shortWords: [String] = []
    for word in wordList {
        if word.count <= maximumLength {
            shortWords.append(word)
        }
    }
    return shortWords
}

Without running anything, choose three calls to this procedure that would convince you it works. Write each call and the result you expect. Then compare with the next pair. Did any of your six calls expose the bug? Which one, and what made it a good choice?

Now pick at least three inputs for your own procedure, and write the output you expect for each. At least one should be at an extreme or on a boundary: an empty list, the smallest possible value, the largest possible value, a value exactly on the edge of your if.

An example

This is a fictional example, here only to show the shape of a plan. It is deliberately simple. Yours must be your own idea, for your own clients.

Part of the app: a screen where the child picks a length with a slider and sees every dinosaur longer than that.

List: allDinosaurs. One item holds a name (String), a length in metres (Int), and whether it ate meat (Bool). Examples: Triceratops, 9, false; Velociraptor, 2, true; Spinosaurus, 15, true.

Why a list: adding a new dinosaur means adding one line to the list. Without a list, every dinosaur would need its own variables and its own code on the screen.

Procedure: keepDinosaurs(longerThan:from:). Parameter: the minimum length the child chose, and the list to search. Returns the dinosaurs longer than that length.

  1. Start with an empty result list.
  2. For each dinosaur in the list:
    • if its length is greater than the minimum length, add it to the result list.
  3. Return the result list.

Called: whenever the child moves the slider.

Test inputs:

Minimum lengthExpected result
0all dinosaurs
9only Spinosaurus (9 is not greater than 9)
20an empty list

In Swift, that plan becomes:

struct Dinosaur {
 
    // MARK: - Stored properties
    let name: String
    let lengthInMetres: Int
    let ateMeat: Bool
 
}
 
let allDinosaurs: [Dinosaur] = [
    Dinosaur(name: "Triceratops", lengthInMetres: 9, ateMeat: false),
    Dinosaur(name: "Velociraptor", lengthInMetres: 2, ateMeat: true),
    Dinosaur(name: "Spinosaurus", lengthInMetres: 15, ateMeat: true),
]
 
func keepDinosaurs(longerThan minimumLength: Int, from dinosaurList: [Dinosaur]) -> [Dinosaur] {
    var longDinosaurs: [Dinosaur] = []
    for dinosaur in dinosaurList {
        if dinosaur.lengthInMetres > minimumLength {
            longDinosaurs.append(dinosaur)
        }
    }
    return longDinosaurs
}

Your plan comes before your Swift. If you change the plan while you build, change it on paper too.

NOTE

In Swift, the first item in a list is at index 0. On the AP Exam, the first item is at index 1. Keep both in mind.