iOS Interview question with answer

Nitin Aggarwal
4 min readFeb 25, 2020

In this article, we will look into some common interview questions asked in many interviews.

Q. What is the defer keyword in Swift?

A defer is a statement that is used for executing code just before transferring program control outside of the scope that the statement appears in.

Defer Example

In the above example, the output will be like this:

Image is going to delete…

Last name has removed…

  • If multiple defer statements appear in the same scope, the order they appear is the reverse of the order they are executed.

The output will be this:

  • defer don’t capture the reference or current value of a variable.

The output will be “Luther King” 2 times.

  • We can’t break the defer execution or we can’t use return, continue, or break inside defer block.

Q. What is CustomStringConvertible?

It is a protocol to allow a type to provide custom textual descriptions of themselves. Every value in Swift is convertible.

Types that conform to the CustomStringConvertible protocol can provide their own representation to be used when converting an instance to a string. The String(describing:) initializer is the preferred way to convert an instance of any type to a string. If the passed instance conforms to CustomStringConvertible, the String(describing:) initializer and the print(_:) function use the instance's custom description property.

Accessing a type’s description property directly or using CustomStringConvertible as a generic constraint is discouraged.

Let’s see an example that how to use this protocol:

Without conforms protocol

The output will be:

Person(name: “Alex John”, address: “California”, age: 25)

With conforms protocol

The output will be:

Alex John is 25 years old and living in California

Prefer way to print the custom description of an instance like:

print(String(describing: Alex))

Note: It is required to conform ‘CustomStringConvertible’ protocol to work with custom ‘description’ output.

Q. What is the difference between Array and Set?

  • An array is a collection of ordered values and Set is a collection of unique values.
  • An array is useful when order is important for you and Set is useful when you need to ensure that a value appears once in Set.

Example:

As we can see in the above example, we have initialized the array and set of string values.

In the output, we can notice that the order of Set is not the same as we initialized. Set is an unordered collection, so it may give you a different order of the result whenever you will access it.

Also, we have initialized Set with some duplicate elements like ‘John’. So in the output, we get unique values. Even if you will print the count of the Set collection with duplicate values, it will return the count as the number of unique elements.

Note: You can not access an element in a set using the ‘subscript’ operator. like namesWithSet[0]. You have to iterate elements to access a particular element.

In the above example, we are trying to add more elements in the array and set. In array, we can use insert() and append() functions to add elements. But in Set, we can use only insert() function.

Note: The values stored in a Set need to conform to the ‘Hashable’ protocol to ensure that each value appears once in a Set.

When you insert an element in a Set, it returns a tuple (inserted, memberAfterInsert). We can ensure that the element has been inserted or not using the ‘inserted’ flag.

To remove an element from a Set, simply we can call remove(member:) function and its return the value that was removed. It's return nil if the value does not exist in the Set.

Power of Sets:

We can perform some other functions on sets also which are not available in Array, some useful functions are:

  • union(): To combine two sets without duplicate values.
  • intersection(): To get the common elements in two sets.

More questions will be adding soon in this article.

Comment your question in the comment box on which you want an answer.

Nitin A

--

--

Nitin Aggarwal

Lead Software Engineer (iOS), Technical Writer, Technical Interviewer, Helping you to crack your iOS interview.