Protocol — The power of Swift

Part-1: What are type-casting and class-types?

Hitendra Solanki
Swift India
5 min readMay 15, 2018

--

Updated on 24th February 2019, 4:51 AM GMT 5:30+

Prerequisites — Any Swifty iDev can read this, who is either beginner or expert. A beginner will gain knowledge and expert will sharpen his or her knowledge by reading this series from start to finish.

We all know Apple’s UIKit, MapKit, and all other frameworks are developed on top of delegation pattern.

Majority of iDevs know that protocol is used for the delegates and data sources to pass and request the data to and from views, view-controller or custom classes, But the real power of protocol is type-casting, AnyObject is a protocol in swift which is used to store any type of object.

Let’s understand type-casting in the simplest way,

Type-casting is the process to convert data-type of a variable or value from one type to some other type.

The simple example of type-casting

Line 1: we have declared one variable of type Int named intValue with value 5
Line 2: another variable of type Double named doubleValue with value 7.0
Line 3: we are doing addition of two Double type values declared before, by converting intValue in to Double value and storing the result into the variable named addition, type of addition will be considered as Double by compiler.

Million dollar question, why do we needs to convert that Int type to Double type during addition?
because Swift is a type-safe language. We are only allowed to do operations on same type of variables/values.

Another question, Why addition is considered as Double implicitly?
because we are declaring the variable and storing value at a same time, so the type of value is considered as type of variable, here our result is generated as Double so addition will be of type Double.

I have one more question please, if above is type-casting then what is the role of protocol here?

Sure, I will give an answer to this question after a few movements, stay tuned. Let’s cover the essential concepts first.

Above is just a simple example of primitive type-casting. Primitive type-casting is the type-casting between primitive data-types provided by a programming language like Int, Float, Double etc.

Another type of type-casting is class-level type-casting, some times referred to as custom type-casting. Class, Struct, Enum are considered as custom types that can be created by a developer like you, custom type-casting plays the key role for inheritance.

Creating sub-class of an existing class is simply known as inheritance.
When you create any view-controller class by taking UIViewController as superclass that is Inheritance.

Do you want to learn more about inheritance? Please check this detailed blog INHERITANCE — The power of object-oriented programming.

What is the role of INHERITANCE in type-casting?

In above example, we have three classes named Person, Employee, Professor.
Employee and Professor are sub-classes of Person class.
We have one more function named displayInformation: which take an argument of type Person.
Line 45,46,47: we are calling displayInformation: function with arguments of type Person, Employee and Professor respectively. This function works without any error here, even we pass argument of type Employee or Professor.

Million dollor question, Why displayInformation: function is working when you pass object of different type in line 46 and 47, while in first example you converted the Int to Double?

Let’s make it simple,
When you create any instance of class Person, that instance has only one class-type(or you can say it data-type or simply type) i.e. Person.
But the instance of class Employee have class-types Employee and Person both, because Employee is a sub-class of Person.

So when you pass instances employee and professor to the function, those are automatically converted to class-type Person, this is also known as implicit type-casting.

Can you say, How may types you will have for the instance of class Professor? (write down your answer in comment section below)

Here is my rule of thumb for possible class types

  • — — — — — — — — — — — —
  • Every instance of the class has #n class-types
  • — — — — — — — — — — — —
  • #n will be 1 for class A when that class is not a subclass of any class
  • [e.g. (class A) — A has not any super class]
  • — — — — — — — — — — — —
  • #n will be 2 for class A when that class is a sub-class of a class AND superclass of that class is not a subclass of any other class
  • [e.g. (class A) -> (class B) — A is a subclass of B, B has not any super class]
  • — — — — — — — — — — — —
  • #n will be 3 for Class A when that class is a subclass of the class AND superclass of that class is also sub-class of a class AND superclass of the superclass of that class is not a sub-class of any other class
  • [e.g. (class A) -> (class B) -> (class C) — A is a subclass of B, Bis a subclass of C, C has not any super class]
  • — — — — — — — — — — — —
  • Similarly, #n will be 4, 5, 6 and so on.
  • — — — — — — — — — — — —

As of now above rule is in the context of sub-classes only, it will be updated when you will have a clear idea about the power of protocols.

Write down your comments below if you need more explanation about “my rule of thumb for possible class types” or in case it seems to complex for you.

Now it’s time to answer your question.

To be Continue…
Part-2 — Type-casting with protocols

--

--

Hitendra Solanki
Swift India

Software Engineer, {Self-taught Developer by passion, Quick Learner, Mentor, blogger, TeamLead} GitHub: https://github.com/hitendradeveloper