iOS App Development with Swift

Harvard Pre-College Program CSCI S-14600, Session I

View the Project on GitHub danallan/swift-precop

Jump to:

Week 0: Day 0, Day 1, Day 2, Day 3, Day 4
Week 1: Day 5, Day 6, Day 7, Day 8, Day 9


This course is an introduction to programming using Swift, Apple's new programming language for Macs and iOS devices. Through Swift, we explore the basics of programming and introduce fundamental computer science topics like abstraction, algorithms, basic data structures, and recursion. The course also features practical programming topics such as scripting, command line interfaces, application programming interfaces, debugging, integrated development environments, understanding documentation, and developing software both in isolation and as a team. By the end, each student designs and implements an app that can be deployed to iOS devices, like an iPhone or an iPad. Students must bring a MacBook, MacBook Air, or MacBook Pro running Mac OS10.11 or higher.


The Summer School is committed to providing an accessible academic community. The Accessibility Services Office offers a variety of accommodations and services to students with documented disabilities.

Academic Integrity

You are responsible for understanding Harvard Summer School policies on academic integrity and how to use sources responsibly. Please be sure to read through the responsibilities, as not knowing the rules or misunderstanding the rules are not acceptable excuses. Please contact the staff if you have any questions about academic honesty.


This course is held during Session I of the 2016 Harvard Pre-College Program which runs from Monday, 27 June through Friday, 8 July. Class is held Monday through Friday from 8:30am to 11:30am in 1 Story Street room 304.

Office Hours

Office Hours are held by one or more TFs from 7PM to 9PM in the Dunster House dining hall on the dates listed below!

Week Days
0 Monday, Tuesday, Wednesday, Thursday
1 Sunday, Tuesday, Wednesday, Thursday

Day 0

Abstraction, algorithms, pseudocode, source code, compilation and interpretation. Swift basics: variables, boolean expressions, logical operators, arithmetic, and control flow.

Documentation and Source

  1. Apple's Swift documentation
  2. Notes from today's class. Thanks, Annie!
  3. Today's source code:


  1. Install Xcode 7.3.1 (on Mac OS X 10.11.5, if possible).
  2. Attempt the below problems. You must complete one, try to complete two!

// Fahrenheit converter

var f = 32
let Celsius = // your code here, using the `f` variable

var c = 10
let Fahrenheit = // your reverse code here


The input should be the number of lines. To get you started, begin with this:

// Diamonds!

let NUM_LINES = 11

// for loops here

Be sure to pay close attention to how many stars are on each line. Use for loops to create this design! You might first try creating pyramid shapes to get started:


Hint: The print() adds a line break to the end of the print statement by default. To turn this off, use the terminator:"" parameter, like so: print("your string here", terminator:"").

Note: Swift doesn't (easily) allow for input from standard in, so don't use the GetLongLong function (which doesn't exist anyway). Instead, just input it as a 64-bit int like so:

let card = 378282246310005

Day 1

Loops, arrays, functions, optionals, tuples.


  1. Diamond staff solutions. Cheng (TF from last year. Thanks, Cheng!), Dan.
  2. Arrays, Functions, Optionals, zip student-supplied solution, modified to include tuples


At a minimum, complete the first three.

  1. Adapt your zip solution (finishing it, if necessary!) to return a type of [(Int?, Int?)], which will allow two lists of different lengths. You may optionally provide names to the tuple, like [(zeroth: Int?, first: Int?)].
  2. Create a simple gradebook program based on dictionaries.
    1. First, read Apple's documentation on dictionaries.
    2. Second, re-familiarize yourself with the gradebook functionality from today's Functions source from class to use dictionaries. Specifically, note that we have implemented two functions: printGradeCount() and average(). We intentionally did not go over dictionaries in class so that you gain some experience reading documentation to achieve a particular programming goal!
    3. Write a program that allows categorization of grades. For example, a class might have exam grades, homework grades, participation grades, etc. You could model this with a dictionary by having a dictionary key represent every category.
    4. For this program, printGradeCount() should accept a dictionary and it should then print the number of grades for each category. average() should accept a dictionary and print the grade average for every category. For example:
    let grades = ["exams": [100, 90], "homework": [50, 85, 67]]

    Should print:

    exams: 2
    homework: 3
    exams: 95
    homework: 67.3333333333333
  3. Download this Playground and follow the directions to complete the problems, which are a collection of functions related to encoding and decoding morse code. Thanks, Hari!
  4. If you still have time, finish completing the Bad Credit exercise from Day 0. Or, if you've already completed it, revisit it and see if you can improve the design of the code, incorporating any lessons from today to make the code more idiomatic.

Day 2

Theory of computation, programming paradigms. Closures, lambdas, higher-order functions.


  1. Staff solutions for zip with optional ints and Morse.
  2. Closures, anonymous functions, and higher-order functions


Provided for fun, each offers a little more information on topics addressed in class.

  1. Halting problem
  2. Vsauce: how to count past infinity


Try to complete all of the below problems.

  1. Create a reduce function that is higher-order. It should accept an array of doubles ([Double]), an initial value i of type Double, and a function f, which itself takes as parameter two Double values and returns a Double. The function calls f first with the initial condition and the first element in the list, and stores the output. It then takes the output, and calls f with the output and the next element in the list, and so on. In short, it combines all of the elements in the list to a single value by relying on a function f to do the combination. Below is a sample of how you might call the function, which when run should return the value 10.0, since it sums the values in the list. reduce([1.0, 2.0, 3.0, 4.0], i: 0.0, f: { $0 + $1 })
  2. Use the mapper, filter, and reduce HOFs to achieve the following:
    1. Multiply all elements of a list together.
    2. Transform an array of values represented in Fahrenheit to Celsius.
    3. Find the largest element of a list.
    4. Transform a double array into it's reciprocal values. Be sure to get rid of 0s! For example: [-0.5, 0, 8] should become to [-2, 0.125]
  3. Write a function called quickSort that takes in an unsorted Double array and outputs a sorted [Double]. Quicksort works by selecting a "pivot" element, partitioning the array into three categories: all elements smaller than or equal to the pivot, the pivot, and all elements larger than the pivot, and then recursively applying quicksort to each category. Because it is recursive, quicksort has a base case when the array size is 1: it should just return the array. As a small hint, this function should use at least one of the higher order functions we discussed in class!
  4. Write a function, called curry, that accepts a function that normally requires two Double parameters (like pow), and allows partial application of that function. In other words, although we'd normally call pow(x, y), after we pass it to curry we should get a new function, exp, that allows exp(x)(y). More specifically, although we'd normally run pow(2, 3), perhaps we only want to partially apply the function as follows:
let exp = curry(pow)
let base = exp(2)
base(3) // outputs 8

Day 3

ASCII, Unicode, representing values with binary and hexadecimal. Aliasing types, generics, inout parameters.


  1. Staff solutions for zip with optional ints and Morse.
  2. Generics, Color, and an obfuscated swap function showing inout (Thanks, Jack, for generics and swap!)


Provided for fun, each offers a little more information on topics addressed in class.

  1. Minute Physics: Computer Color is Broken


Complete at least the first two problems of this Day 3 Homework playground!

You may find the following YouTube videos useful to understand the homework problems:

  1. CS50: Caesar cipher
  2. CS50: Vigenère cipher

Day 4

Basic encryption. Algorithmic complexity, parallelism. Game of Fifteen, Xcode, iOS, Storyboards, constraints.


  1. Solutions to Day 3 homework
  2. MapReduce (includes exercises)
  3. Hello World iOS Application
  4. Gallery app. Loads images over the internet; we will cover this in more detail on Tuesday.


Attempt MapReduce exercise. Begin Game of Fifteen exercises. You may choose how far along you wish to complete the Game of Fifteen exercises, but keep in mind: the more of these you do now, the more time you will have to polish your Game of Fifteen iOS app next week!

  1. MapReduce (exercises include in source code showed in class). Pay close attention to the instructions to discover how to set up the aliceinwonderland.txt file so it can be discovered by the Playground.
  2. Game of Fifteen exercises
  3. Send all homework for Week 0 (Days [0,3]: Monday through Thursday) to Dan in zip files. If you have already sent, thank you! No need to do so again.

Day 5

4 July, 2016: Independence Day holiday

Day 6

Swift classes, enums, and structs. iOS, continued, and the application lifecycle, multi-threaded applications. Programmatic creation of UI elements.


  1. Updated and discussed day 4's gallery app.
  2. Skeleton code for Game of Fifteen


Having now switched to project mode, we're working towards implementing a Game of Fifteen app for iOS.

  1. Take skeleton code from class and attempt to put constraints on the game board so that it appears square and fully centered on the device screen, sized appropriately for the device.
  2. Create the 15 tiles programmatically, sizing them correctly inside the game board.
  3. Continue working on Game of Fifteen exercises from day 4, if you haven't yet finished them.

Day 7

Debugging. UI element practice including bounds, frames, animation, timers.


  1. Staff class that provides isBoardSolvable()
  2. Animations, countdown, alerts


Continue working on Game of Fifteen project, bundling the exercises into an app, drawing the board, and connecting the elements.

Day 8

UI elements, continued: alerts and action sheets. Placing apps on your phone.

Compile your app on phone

  1. Create a free Apple Developer account.
  2. Fetch your device's identifier from Xcode: connect your phone to your computer with a USB cable, unlock it and tap "Trust". On your computer, Open Xcode, click the "Window" menu and open "Devices", select your device, and copy the full string named "Identifier".
  3. Submit both the email address you used to create the Developer account and the device's identifier to this Google form. We'll then invite you.


Complete and put finishing touches on your Game of Fifteen app!

Day 9



  1. Look at Annie's phenomenal write-up on women in Computer Science, which includes resources and a personal perspective on being a minority in the field.
  2. Download the staff solution to the Game of Fifteen, which we've called nPuzzle since it can generate boards of different sizes. Compare how we implemented the project compared to your own work!


Dan Dan Armendariz Instructor Email Web, Instagram
Hari Hari Anbarasu Teaching Fellow Email Facebook
Annie Annie Chen Teaching Fellow Email Chrome Store
Jack Jack Deschler Teaching Fellow Email  
AJ Arturo J. Real Teaching Fellow Email Twitter
Stelios Stelios Rousoglou Teaching Fellow Email LinkedIn, Facebook