The concept of generics has been around for a while, so it should not be a new concept for developers coming from languages such as Java or C#. Swift's implementation of generics is very similar to that of these languages. For those developers coming from other languages, such as Objective-C, which do not have generics, they might seem a bit foreign at first, but once you start using them you will realize how powerful they are.
Generics allow us to write very flexible and reusable code that avoids duplication. With a type-safe language, such as Swift, we often need to write functions or types that are valid for multiple types. For example, we might need to write a function that swaps the values of two variables; however, we may use this function to swap two String
types, two Int
types, and two Double
types. Without generics, we would need to write three separate functions; however, with generics, we can write one generic function to provide the swap functionality...