Skip to content

iOS Development – Week 3

That was the end of week 3 – yes I am a bit late posting this… We continued learning Swift, this time it was all about functions, types, closures and advanced swift features. I think closures is one of the most important things we discussed. So, here we go:


  • Think of them as “blocks of code that do something
  • Differences between closures and functions:
NoArgument labels?Yes
NoDefault parameters?Yes
YesWrite Inline?No
{ (a: Int, b: Int) -> Int in a * b }Different syntaxfunc multiply(a: Int, b: Int) -> Int { a * b }

We have also covered some useful higher order functions:

  • forEach – Always runs on all elements, you can pass functions and chain with other methods. Notice that a for loop can use break and continue and where clauses. These do not work with forEach
  • map – Goes through all elements of a collection, operate on every single element, and return a new collection with the results.
  • compactMap – Only returns collection elements that are not nil
  • flatMap – Lets us handle multidimensional arrays. It expects an array as input
  • filter – Filters out the elements that meet the required conditions
  • reduce – Takes two arguments: an initial value and a closure

Properties and Methods

  • Properties – What a type has
  • Methods – What a type can do

We also talked about:

  • Stored properties – variables and constants defined in and object.
  • Stored Type Properties – Stored with the type instead the instances.
Stored Instance PropertiesStored Type Properties
EnumNo – Instead have computed instance properties!Yes

Stored properties maintain their value every time we access the property.

A computed property changes it every time we access it.

Lazy properties only get assigned at the time they are needed. Note that they keep the values attached to them at initialisation time!! Lazy variables are being something in between constants that get set at initialisation, and computed properties that get evaluated every time you call them.

Use lazy variables for constants that:

  • Are expensive to calculate
  • Might not be used by every instance

Protocols & Inheritance

Finally we also talked about protocols and inheritance. Protocols define a set of requirements that a type must meet. You might think of a protocol like a certification or stamp that says “This type is certified type iterable” or “This type is approved to represent a view”.

A protocol can be adopted by any named type, including other protocols, as long as they meet the requirements of that protocol.

Jelly Belly – Update

The aspects explored above lead to a series of exercises to implement overloading, closures and protocols. We also were asked to implement some of this to our app. In my case, to make a suitable transition of some of the logic from the assignments to the implementations I made during Week 2, I started looking at extending some of the data structures I had. I was looking at expanding some of the implementations I had for my Dish and Order structs (actually I changed one to a class so I could your mutability). The entries in bold in the data structures diagram include changes to be made to the app. See the integrated entry in Figma here.

The new Dish class has an optional called discountable to indicate that the dish can be discounted. The idea is that not all dishes are expected to have a discount, for example drinks may not be able to be discounted. A lazy property called discountFactor enables us to compute the finalCost – which is the cost of the dish after discount (if dish is discountable).

Week 4 – here we go!

Related Posts


1 thought on “iOS Development – Week 3”

  1. Pingback: iOS Development – Week 1 – Quantum Tunnel

Comments are closed.