Skip to content

iOS Development – Week 2

Well, that was week 2 of the iOS development bootcamp I joined. This time the focus was on getting to grips (or revisiting) some of the fundamentals of the Swift programming language. We have been revising some of the following topics:

One aspect that has been emphasised is the use of optionals. As mentioned by the Apple Developer Documentation:

Optional -A type that represents either a wrapped value or nil, the absence of a value.

This means that we can use an optional when we want a variable or constant to be able to contain no value! In other words an optional type may contain a value or an absent value (nil). This may be like having a box that may or may not contain a cat… You only find out when you need to look in the box. If the cat is in the box you can then feed him, but if the cat is not there and you have not handled the situation… well panic ensues.

An optional data type is declared by appending ? to the Type. If an optional contains a value in it, it returns value as Optional<Value>, if not it returns nil.

var myValue:Int?
var anotherValue:Int?

Running the code above will print nil for both variables to the console because we have not initialised the variables with a value. They can contain an integer or no value. If we try to use the variable when it has no value we get an error. Instead we needs to unwrapped the variables or constants. This can be done safely with the help of an if let statement. If the optional has a value we use it, if not we can handle the situation better:

var myValue:Int?
if let temp = myValue {
    print("Success, we have the value \(temp)") 
} else {
    print("Failure... no value stored")

Nil-coalescing operator

What a cool name! So what is it?? Well the nil-coalescing operator is used to check whether an optional contains a value or not. We can think of this as a shorthand for an if statement. For an optional a we can use the nil-coalescing operator as follows: a ?? b). If a contains a value then it returns that, but if not then we return the default value b.

var myValue:Int?
let defaultVal = 5
let unwrappedVar:Int = myValue ?? defaultVal

The result of the code above is printing 5 to the console. Pretty neat, right‽


The assignment for this week is a series of tasks that need to be implemented in a Swift playground. Similar to the playgrounds you can create with the Playgrounds app, but in Xcode. I guess you can think of them as being similar to a Jupyter notebook – you can add markdown to document things in a nicer looking way and you can execute the code on demand.

The tasks lead to the implementation of a data structure that can be used in the capstone project we are building. In the case of “Jelly Belly” I have implemented a series of data structures that enable me to build a Dish struct that contains information about dishes served in the restaurant: Name, ingredients, cuisine, etc.

Each dish can be then added to an Order struct that can be used to display the information for each dish in the order and calculate the total bill for the order. Here are some screenshots of the end result in the Xcode simulator:

Related Posts

Also published on Medium.


2 thoughts on “iOS Development – Week 2”

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

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

Comments are closed.