iOS — Computer Science Fundamentals

Filip Varda
9 min readMay 25, 2021

In this article, we will go through some Computer science fundamentals. As a developer, you should have a strong knowledge base when it comes to CS. We will start with more popular terms such as file or class, and we will work our way up to more complex ones.

Let’s start with something that all of us create every day. I’m sure that you have thousands, if not millions, on your PC right now.

What is a File?

A file is a named location that stores data and information. Files are always stored on a storage device (for example USB) using a file name. Files have the primary and secondary parts of the name separated by the “.” dot.

As a developer, the first thing you do after setting up your new projects is creating a new file. In that file, most likely you will create your first class.

What is a View?

Views are the visual building blocks of your app’s user interface. A view represents a single item on the user interface. Views can detect when a user interacts with them, such as a tap on a button control, but as those interactions happen, views send messages to other objects in your app, usually a view controller, to handle the processing.

To keep it simple, everything is a view, buttons, tables views, scroll views, cells, etc.

What is a Class?

A class is a blueprint from which objects are created. Classes usually have constructors. Here is an example of a class:

class Number {
var a: Int?
}

A simple class called Number with a variable named a. This is our blueprint from which we can create an object. To do so, in Swift, we have to write this part of the code:

let number = Number()

Here we are using a default constructor to create a new instance of an object that is of type Number.

What is a Constructor?

A constructor is a method that is used to create an Object of class. There are two types of constructors: default and parameterized constructors.

In the example above, we used a default constructor. This will create an object of type Number, but the object’s variables will be nil, aka. null.

A custom parameterized constructor is usually defined in the class, looking something like this:

class Number {
var a: Int?
convenience init(a: Int) {
self.init()
self.a = a
}
}

Here is how we use this constructor in a view controller:

class NumbersController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var number = Number(a: 5)
}
}

Here we will initialize an object of Number, but this time the variable of the object, that is a, will have a value of 5.

What is a structure and is it different from a class?

Structures, or structs, are one of the named types in Swift that allow you to encapsulate related properties and behaviors. You can define it, give it a name and then use it in your code.

For example:

struct Pizza {
var slicesOfHam: Int
var slicesOfCheese: Int
}

We talked about constructors, and an interesting fact about structures is that when you define it in Swift, it automatically creates a parameterized constructor for you, so you can use it anywhere you want.

When compared to a class, a structure is a value type, while a class is a reference type, meaning that when you copy a structure, you will end up with two unique copies of the data. When you copy a class, however, you end up with two references to one instance of the data.

What is a variable and what types of variables are there?

A variable provides us with named storage that our programs can manipulate. Each variable in Swift has a specific type that determines the size and layout of the variable’s memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.

Swift supports the following basic types of variables: Int/UInt, Float, Double, Bool, String, and Character. Swift also allows defining various other types of variables, but we won’t cover them in this article, such as Optional, Array, Dictionaries, Structures, and Classes.

For example, the variable that we declared in our Number class is of type Int.

What is a Tuplet?

Tuples in Swift occupy the space between dictionaries and structures. They hold very specific types of data (like a struct) but can be created on the fly (like dictionaries). They are commonly used to return multiple values from a function call.

Here is an example of tuple declaration:

var tupleName = ("Some string", 34, Number(a: 5))

Tuples can hold multiple value types. In this example, we have 3 types, String, Int, and Number, that we mentioned before.

What is KVO and how is it different from delegates?

KVO, which is Key-Value observing, is a Cocoa programming pattern you use to notify objects about changes to properties of other objects. It’s useful for communicating changes between logically separated parts of your app, such as between models and views. You can only use key-value observing with classes that inherit from NSObject.

The difference between a KVO and a delegate is that KVO should be used to observe changes in property for an object. Delegates should be used when there are lots of callbacks and only one object is desired to be notified. Delegates are objects that act as another program when an event fires.

What is ARC and how does it work?

ARC or Automated Reference Counting is used by Swift to track your app’s memory usage. In most cases, this means that memory management “just works” in Swift, and you don’t need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed. Sometimes ARC needs a bit more info regarding your properties (for example, weak and strong references).

ViewController life cycle

In Swift, the lifecycle of a view controller looks something like this:

init() -> viewDidLoad() -> viewWillAppear() -> viewDidAppear() -> viewWillDisappear() -> viewDidDisapear() -> deinit()

This is a basic life cycle of a view controller. I won’t go into any details regarding what does each method does, but I will mention that there are more methods that you might use.

  • loadView()

This is the method that creates the view for the view controller. You override this method only in case you want to build the whole interface for the view controller from code. Don’t use this unless there is a valid reason. If you are working with storyboards or nib files you do not have to do anything with this method and you can ignore it. Its implementation in UIVIewController loads the interface from the interface file and connects all the outlets and actions for you.

  • viewWillLayoutSubViews()

This one is called to notify the view controller that its view is about to layout its subviews. This method is called every time the frame changes like for example when rotating or it’s marked as needing layout. It’s the first step where the view bounds are final. If you are not using autoresizing masks or constraints and the view size changes you probably want to update the subviews here.

  • viewDidLayoutSubviews()

Called to notify the view controller that its view has just laid out its subviews. You can make additional changes here, after the view lays out its subviews.

  • didReceiveMemoryWarning()

iOS devices have a limited amount of memory and power. When the memory starts to fill up, iOS does not use its limited hard disk space to move data out of the memory like a computer does. For this reason, you are responsible to keep the memory footprint of your app low. If your app starts using too much memory, iOS will notify it. Since view controllers perform resource management, these notifications are delivered to them through this method. In this way, you can take action to free some memory. Keep in mind that if you ignore memory warnings and the memory used by your app goes over a certain threshold, iOS will end your app. This will look like a crash to the user and should be avoided.

iOS application lifecycle

The iOS operating system manages the application states, but the app is responsible for handling user experience through these state transitions. There are five states of the app:

  • Not Running

Either the application has not started yet or was running and has been terminated by the system.

  • Inactive

An application is running in the Foreground but is not receiving any events. This could happen in case a Call or Message is received. An application could also stay in this state while in transition to a different state. In this State, we can not interact with the app’s UI.

  • Active

An application is running in the Foreground and receiving the events. This is the normal mode for the Foreground apps. The only way to go to or from the Active state is through the Inactive state. The user normally interacts with UI and can see the response/result for user actions.

  • Background

An application is running in the background and executing the code. Freshly launching apps directly enter into In-Active state and then to Active state. Apps that are suspended, will come back to this background state, and then transition to In-Active → Active states. In addition, an application being launched directly into the background enters this state instead of the inactive state.

  • Suspended

An application is in the background but is not executing the code. The system moves the application to this state automatically and does not notify. In case of low memory, the system may purge suspended applications without notice to make free space for the foreground application. Usually, after 5 secs spent in the background, apps will transition to Suspend state, but we can extend the time if the app needs.

UIApplication object defines some methods which are called or will be responded to some of the above states which are most important, to let us work on those transition states regarding our app functionalities. As soon as the application has successfully initiated the launch process:

  • application:willFinishLaunchingWithOptions

This method is called after your application has been launched successfully. It is the first method from our app delegate, which will be called. You can execute your code if the launch was successful.

  • application:didFinishLaunchingWithOptions

This method is called before the app’s window is displayed. You can finalize your interface and can provide the root ViewController to the window.

  • applicationDidBecomeActive

This method is either called to let your app know that it moved from the inactive to active state or your app was launched by the user or the system or in case the user ignores an interruption (such as an incoming phone call or SMS message) that sent the application temporarily to the inactive state. You should use this method to restart any tasks that were paused (or not yet started) while the app was inactive.

  • applicationWillResignActive

This method is called to let your app know that it is about to move from an active to inactive state. This can happen in case of any interruptions (such as an incoming phone call or SMS message or Calendar alerts) or when the user quits the app. You should use this method to pause any ongoing tasks or disable timers etc.

  • applicationDidEnterBackground

This method is called to let the app know that it is not running in the foreground. You have approximately five seconds to perform any tasks and return back. In case you need additional time, you can request additional execution time from the system by calling beginBackgroundTask(expirationHandler:). If the method does not return before time runs out your app is terminated and purged from memory.

  • applicationWillEnterForeground

This method is called as a part of the transition from the background to the active state. You should use this to undo any change you made to your app upon entering the background. applicationDidBecomeActive method is called soon after this method has finished its execution which then moves the app from the inactive to the active state.

  • applicationWillTerminate

This method is called to let you know that your app is about to terminate. You should use this method to perform any final clean-up task. You have approximately five seconds to perform any tasks and return back. If the method does not return before time expires, the system may kill the process altogether. This method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason. You shouldn’t wait for applicationWillTerminate to be called in order to save your data. There are some cases when applicationWillTerminate won’t be called before app termination. For example, the system will not call applicationWillTerminate when the device reboots.

And with that, we will end this article. I think that this is a great article for every developer, even the senior ones because we all need to refresh our memory here and there. I hope this helps you on your journey as a software engineer! Cheers!

--

--

Filip Varda

Freelance Senior iOS Developer. If you have any questions about my articles or iOS development you are welcome to reach out on my email filip.varda.zg@gmail.com