Preparing for the Swift Interview-1

Hey folks! Today, we will cover some of the subjects you might encounter in interviews. I’ve been doing interviews for a while. I’m trying to gain experience in this regard. When I think about my first interview (it was terrible) I can see that I have come a long way. After all interviews, I discerned that I can at least more or less predict the questions that may come my way. In this article, I will tell you what topics they are.

We can consider protocols individually or under OOP. Let’s talk briefly about what it is. Protocols are a blueprint of methods or properties that can be adopted by classes or other types. A protocol can inherit more than one protocol and write extensions to the protocols.

Supposing that we have a protocol for which we calculate the area. This protocol gives us a gettable property and a method.

We made the Area class conform to the AreaProtocol. This means that we can use the properties and methods provided by the protocol in the class.

We can also reach the result with an object that we will create from the Area class.

  • Multiple Protocols

Now let’s take a look at multiple protocols.

As seen in the example, a class can be made compatible with more than one protocol.

  • Protocol Inheritance

In the example, we see that the HouseType protocol inherits the House protocol. The Apartment class only conforms to the HouseType protocol, but since HouseType inherits House, we need to implement the properties of both protocols in the class. In addition, a protocol can inherit more than one protocol.

  • Protocol Extension

As we mentioned above, we can extend protocols.

In the above example, we have defined a protocol for which we get the name property. We extended it and added the uppercase function.

Output:

lowercase name: damlauppercase name: DAMLA

It allows us to create functions or classes that can be used with different data types. It makes the code reusable.

  • Generic Functions

Output:

Data Passed: swiftData Passed: 5

We have created a generic function with <T> type parameter and it will work as whatever data type we pass to this function.

  • Generic Class

Output:

Generic class returns: trueGeneric class returns: swift

In the example, we created a generic class named TypeInfo. This class can be used to work with any data type. We worked with bool and string data types in the example.

A function name is a special function without a name. Closures can capture or store references to any constant and variable.

Swift Document
  • Closure may not take parameters and may not return anything.
  • Closure can take a parameter and return a parameter.
  • Trailing Closures

If a function takes closure as its last parameter, we can use the closure as the function body without specifying the parameter’s name.

Output:

area of the square: 16
  • Non-escaping Closures

If closure is marked with the non-escaping parameter, it means that the closure cannot escape the body of the function. When we use closures as function arguments, the closure is executed with the body of the function and returns to the compiler. When the execution ends, the closure ceases to exist in memory.

Closure parameters are non-escaping by default. To escape closure execution, we need to mark the closure as escaping.

  • Escaping Closures

Marking closure as escaping means we must use self inside the closure. The closure is passed to the function as an argument and is called after the function returns.

That’s all I’m going to talk about in this article. In the following, we will talk about other important points. See you in the next article.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store