Swift Generics

Chameleon in Programming

Sridharan T
IVYMobility TechBytes
3 min readJul 7, 2020

--

Generics

In swift, Generics are the parameterized types. By using generics we can create flexible and reusable functions or methods that work with any type. In swift class, structure or any method that operates on parameterized types called a generics.

Generally in swift, by using generics we can avoid a duplicate code by writing reusable functions or methods in a clear and abstracted manner based on our requirements.

The concept of generics is powerful in swift and the standard libraries like arrays or dictionaries of swift built by using generic code. In swift, when we create an array, it can hold any type of data like integer, string, or any other types the same way dictionaries are also used to hold any type of data as a key-value pair.

Here is the simple non-generic function example which shows how generics will help in the Swift programming language.

In the above example, we have defined two functions swapInteger and swapString separately. Because swapInteger function will work only for integers and swapString will work only for strings.

When we run the above example, the output will be

After Swapping firstNumber: 3334, secondNumber: 445After Swapping firstString: Orange, secondString: Apple

In swift, if we use non-generic code we need to create separate methods or functions based on the type of data due to that redundant code will increase in the application.

If we use generics, we can create a single method or function that can accommodate any type of data.

Generic Functions & Type Parameters

In swift, Generics will work with any type for that need to add <T> after the function name.

Now we will implement a generic version for the above example which we used to swap integers and strings.

In the above example, we defined a single function with a generic placeholder <T> to swap any type of values like integer, string, double.

The placeholder T doesn’t say anything, but it does say that both a and b must be of the same type T, whatever T represents. The actual type to use in place of T is determined each time the SwapValues(_ :, _ : ) function calls.

We can call SwapValues() function by passing two values of any type, as long as both of these values are of the same type as each other.

When we run the above code, the output will be

After Swapping firstNumber: 3334, secondNumber: 445After Swapping firstString: Orange, secondString: Apple

Based on the above result we can clearly say that Generics will help us to write a flexible and reusable code to perform our operations in applications based on our requirements.

  • Swift provides us more flexibility to define our own custom generics types in generic functions. The custom generic types are classes, enumerations, and structures that can work with any type like arrays and dictionaries.
  • The implementation of generics types is mostly in Data Structures and mainly used in a stack. I have already discussed this topic in my previous blog. Here is the link, read it out

--

--