Say Hello To Swift Generic

Swift is not a baby anymore. It has been three years since the Swift was released. Generic is the most powerful and advanced feature of Swift. If you have not been introduced to Swift generic yet, then definitely you are missing one of the most beautiful things. In this article, we will try to understand generic in a simple way and we will see how we can beautify our Swift codes with generics.  
  • Expertise level: Intermediate
  • XCode: 9.0
  • Swift: 4.0 
As per apple docs, " Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner. "
Confusing? Let's simplify it with an example.
Let's take an example in which we have to create a function which will take input number of pencils and pens added to the cart. And print a statement. 
  1. func cartDetailsString(numberOfPen:Int , numberOfPencil:Int) -> String{  
  2.     return ("Dear you have added \(numberOfPen) pen and \(numberOfPencil) pencil in your cart")  
  3. }  
Then, call the function and see the output.
  1. cartDetailsString(numberOfPen: 3, numberOfPencil: 5)  
  3. output: Dear you have added 3 pen and 5 pencil in you cart

Now, we have a situation;  the requirement is that we also want to pass a string in the argument. I mean we can pass an integer (3 / 5) or string (three / five). So that we can print.

  1. Dear you have added 3 pen and 5 pencil in you cart
  2. or
  3. Dear you have added three pen and five pencil in you cart

Here, we have one solution

Write the same function again which will take the argument as a string and print the required output. Or we can optimize the same function which can take different DataTypes as an argument and give required output.
Seriously? How? 
Time to feel the beauty of Generic!

Let's rewrite the same function with the help of generic which can take a different argument as a parameter. 
  1. func cartDetailsString<T>(numberOfPen:T , numberOfPencil:T) -> String{  
  2.     return ("Dear you have added \(numberOfPen) pen and \(numberOfPencil) pencil in you cart")  
  3. }  
Let's call the function and see the output.
  1. // pass argument as Int  
  2. print(cartDetailsString(numberOfPen: 3, numberOfPencil: 5))  
  3. Output: Dear you have added 3 pen and 5 pencil in you cart
  1. // pass argument as String  
  2. print(cartDetailsString(numberOfPen: "three", numberOfPencil: "five"))
  3. Output: 

    Dear you have added three pen and five pencil in you cart

The generic version of the function uses a placeholder type name (called T here) instead of actual type (such as string , int , or any data type). The placeholder type does not say anything of what T must be, but it says that both the parameter must be of the same type. And the type is determined each time the function is called.
The function name is followed by angle bracket and within that, the placeholder text. like <T> . The bracket tells Swift that T is the placeholder type name.  
Generics Are Everywhere 

Much of the Swift standard library is built with generic code. If you are coding with Swift, you are using generic without knowing it.
For example, Swift array and dictionary both are the generic collection types. We can create an array that can hold integer, string, float, double etc. so with a dictionary. 
Another Example - Just think how + function takes an input of different data types and gives output. There is generic behind that. 

Generic is love. Generic is beautiful. It helps us to refactor our code and makes it more reusable. If you love generic, then check  another article I wrote and see how I used the generic with a protocol.  If you have any doubts, let me know in the comments.
Happy Programming :) !!!