Protocol — The power of Swift

Part-4 — My rule of thumb for possible data types

Hitendra Solanki
2 min readJul 14, 2018

Updated on 17th December 2018, 1:33 AM GMT 5:30+

Welcome to the 4th part of this series, In-case you missed previous parts, I will strongly suggest you that first coverup the essential concepts from
Part-1: What is type-casting and class-types?
Part-2 — confirming a protocol
Part-3 — Protocol composition

Before we dive deep into the understanding of my thumb rule of data types, let’s first clear the concept of AnyObject.

What is AnyObject?

AnyObject is an empty protocol available in Swift. The protocol to which all classes implicitly conform.

We can store any class instance with AnyObject type.

store any class instance with AnyObject type

Above example will work without any compiler error, because the class Person will have two type i.e. Person and AnyObject.

So now we can say that every class have #(n+1) types,
#n types as per my previous rule of class types and one additional type i.e. AnyObject.

If class is also confirming some protocols say 4 protocols, class will have
#(n+1+4) types,
#n types as per my previous rule of class types and one additional type i.e. AnyObject plus 4 protocols type that class is confirming.

--

--

Hitendra Solanki

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