Generic code is used to write reusable and flexible functionalities that can deal with any type of variables. This helps in writing reusable and clean code regardless of the type of objects your generic code deals with. An example of using generics is when you use
Dictionary. You can create an array of
String or any type you want. That's because
Array is natively created and can deal with any type. Swift gives you the ability to write generic code very easily as you will see in this section.
Before learning how to write generic code, let's see an example of a problem that generics solve. I bet you are familiar with stack data structures and have been using it in one ...