Swift 5.2 Generics Explained - How to Use Generics in iOS Apps


Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code. In fact, you’ve been using generics throughout the Language Guide, even if you didn’t realize it. For example, Swift’s Array and Dictionary types are both generic collections. You can create an array that holds Int values, or an array that holds String values, or indeed an array for any other type that can be created in Swift. Similarly, you can create a dictionary to store values of any specified type, and there are no limitations on what that type can be.

The Problem That Generics Solve

Let’ s say, we have to print array of integers and strings. I can create 2 functions to do this work.

Now I have to print array of floats, or array of custom objects. If we look at above functions, only difference is in type used. So instead of repeating code we can write reusable generic function.

Generic Functions

A method which is capable of taking different datatypes and perform operation called genric function.

Generic function can work with any type, identified by the placeholder type T. The placeholder type name doesn’t say anything about what T must be, but it does say that both printGenric must be of the type T,whatever T represents. The actual type to use in place of T is determined each time the printGenric(_:) function is called.

Here , instead of two methods to print int string as in the previous code sample, we created one generic method which can printGenric array with any type values.

placeholder type

The placeholder type T in above example is a type parameter. You can provide more than one type parameter by writing multiple type parameter names within the angle brackets, separated by commas.

👉Always give type parameters upper camel case names (such as T and TypeParameter) to indicate that they’re a placeholder for a type, not a value.

Let’s look at another example. Let’s say you’ve created a simple function that adds one number to another. Like this:

Protocols & Associated Types in Generics

Here we will see, Associated types are a powerful way of making protocols generic, An associated type gives a placeholder name to a type that is used as part of the protocol. The actual type to use for that associated type isn’t specified until the protocol is adopted. Associated types are specified with the associatedtype keyword.

The above snippet shows the declaration of ItemStoring protocol ItemStoring declares an `associatedtype` called Datatype which can store items in an array. What type those items are depends on whatever conforms to the protocol.

Let’s implement the ItemStoring protocol in a AppendData struct. This AppendData will be able to store any kind of item.

See how we’re using Items in the struct declaration? We’re not specifying an actual type – just a placeholder. The AppendData struct has a simple array that can store and retrieve items.

The generic ItemStoring protocol only specifies that whatever class /struct adopts it needs to include a function to store any item, and retrieve any item.

If you enjoyed this article please share.
Thanks for reading!!

Post a Comment