iOS Protocols with Associated Type
You have come accross protocol & it's functionalities, you will be addicted to it.
Now we will learn about associatedtype in protocol.
We can create class as Person
You can just call like
Output:
protocol forces class/struct to work with String. But, what if you want property to be Int or Bool.
For this swift has introduced Protocol Associated Types.
In General we can create struct as generic type
But when you come to protocol we have to use associatedtype.
Let try with above Details example:
"Associated type = type alias + generics"
also you can create a class/struct with some other datatype.
Now you can just call
Output:
For Person1
Output:
Download the sample here
Reference:
License
AssociatedType.playground is distributed under the MIT license .
Let's grow together 🌱
Cheers 🍻