Top iOS interview questions and answers 2022

Sunee Ragu
6 min readSep 25, 2022

--

1. What is the Difference Between Structures and Classes?

One of the most important differences between structures and classes is that structures are value types and are always copied when they are passed around in your code, and classes are reference type and are passed by reference.

classes have Inheritance which allows one class to inherit the characteristics of another.

Struct properties are stored on Stack and Class instances are stored on Heap, the stack is faster than a class.

Struct gets a default initializer automatically whereas in Class, we have to initialize.

Struct is thread safe or singleton at any point of time.

To summarize the difference between structs and classes, it is necessary to understand the difference between value and reference types.

  1. When you make a copy of a value type, it copies all the data from the thing you are copying into the new variable. They are 2 separate things and changing one does not affect the other.
  2. When you make a copy of a reference type, the new variable refers to the same memory location as the thing you are copying. This means that changing one will change the other since they both refer to the same memory location. The sample code below could be taken as reference.

2. Storyboard Vs Programmatically creating UI for iOS

1. Ease of use

Storyboards are straight-forward and easy to use. For example, If I needed to create a new view controller with buttons, labels, textfields, or segues, it is fairly easy to implement. With a few clicks, drag, and drop. For programmatically creating a new view needed to write more lines of code. this can get quite tedious. It is important to include translatesAutoresizingMaskIntoConstraints and .active = true , or view will not show.

2. Visual

One of the major advantage of using storyboard is that it gives you a very good visual representation of the application. You know how every button, label, textfields, and view will look like. With a few clicks and adjustment, can easily create a whole view.You know how every button, label, textfields, and view will look like. With a few clicks and adjustment, can easily create a whole view. Additionally constraints are visually presented as well, any adjustment to constraints will show in a different color.

Programmatic

1. Control

Coding all of your UI elements will give a sense of control. Anything that is achievable through StoryBoard, can do the same through code. For example if I decided to make one of my font bold. It is easy to change settings in code I know exactly where I placed all the UI elements.

2. Reusability

Can reuse the codes throughout our project. If I wanted to create labels inside my view, I can just copy most of that code, and make a few minor adjustments.

3. Clutter and Navigation

In Storyboard’s initial phase, this might not seem to be a problem, but once it’s filled with UI elements and controllers, it is very difficult to navigate.

4. Merge Conflict

When we are working in a team with other developers, complications starts to arise when we decided to merge our work on GitHub. It is very difficult to resolved the merge conflict. It is not a simple task because our Storyboard merge conflicts consists more than just code, there are codes that renders the UI.

3. Data Persistance iOS

Different types of data can be saved to the device locally, using different tools (Swift APIs). To achieve that in Swift that we can use

· UserDefaults

· Keychain

· Saving files to disk(FileManager)

· Core Data

· SQLite

· Property Lists(Plist)

UserDefaults to store small pieces of information(e.g. whether the user prefers light or dark mode, at what time they want to receive a daily reminder, whether they actually want to receive notifications, etc.). UserDefaults can be retrieved by using the standard static property, a singleton that gives easy access to this storage device.UserDefaults work as a key-value storage, with Strings as keys.

Keychain used when you need to save sensitive user data, such as passwords or login credentials. Apple Keychain is a very popular and functional Swift tool that every iOS and MacOS user is using. It can be used to save passwords, secure notes, certificates, etc. In general, Keychain is an encrypted database with quite a complicated and robust API.

Saving files to disk(FileManager) Apple makes writing, reading, and editing files inside the iOS applications very easy. Every application has a sandbox directory where you can store your files. FileManager object provides all these functionalities with very simple APIs. Files can be stored inside catalogs and sub-catalogs of your app’s Document directory. That’s why Apple recommends using the URL to specify and work with files using the FileManager object. URL classes, in general, are used when working with a network like API requests and opening the websites.

Core data is one of the most powerful framework provided by Apple for macOS and iOS apps. Core data is used for handling the model layer object in our application. Can treat core data as a framework to filter, modify,, save, track the data within the iOS apps. Core data is not a relational database. Using core data, we can easily map the objects in our app to the table records in the database.

SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. SQLite is the most used database engine in the world. SQLite is a relational database management system. SQLite is not a client-server database engine. In fact, it is embedded in the client application itself. So there is no networking involved in SQLite.

Property list (Plists) in Swift : Plist files enable storing serialized objects with key/value conventions. In macOS and iOS applications, the most common usage example of plist is an Info.plist file which stores application settings values. Inside Info.plist we can find the application name, bundle identifier, version number, required permissions info, Facebook App info, etc.

4. What are generics and which problem do they solve?

In Swift, you can use generics in both functions and data types, e.g. in classes, structures or enumerations. Generics solve the problem of code duplication. When you have a method that takes one type of parameter, it’s common to duplicate it to accommodate a parameter of a different type.

For example:

func areIntEqual(_ x: Int, _ y: Int) -> Bool {

return x == y

}

func areStringsEqual(_ x: String, _ y: String) -> Bool {

return

areStringsEqual(“iOS”, “iOS”) // true

areIntEqual(1, 1) // true

By adopting generics, you can combine the two functions into one and keep type safety at the same time. Here’s the generic implementation:

func areTheyEqual<T: Equatable>(_ x: T, _ y: T) -> Bool {

return x == y

}

areTheyEqual(“iOS”, “iOS””)

areTheyEqual(1, 1)

Since you’re testing equality in this case, you restrict the parameters to any type that implements the Equatable protocol. This code achieves the intended result and prevents passing parameters of a different type.

5. Describe screen life cycle?

iOS calls the UIViewController methods as follows:

viewDidLoad() —

Called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. The view controller’s outlets are guaranteed to have valid values by the time this method is called.

viewWillAppear()

Called just before the view controller’s content view is added to the app’s view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen.

viewDidAppear()

Called just after the view controller’s content view has been added to the app’s view hierarchy. Use this method to trigger any operations that need to occur as soon as the view is presented onscreen, such as fetching data or showing an animation.

viewWillDisappear()

Called just before the view controller’s content view is removed from the app’s view hierarchy. Use this method to perform cleanup tasks like committing changes or resigning the first responder status.

viewDidDisappear()

Called just after the view controller’s content view has been removed from the app’s view hierarchy. Use this method to perform additional teardown activities.

--

--

Sunee Ragu

I am iOS developer. I am quick learner and passionate in building apps.