From the course: Swift 5: Protocol-Oriented Programming

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Working with generic types

Working with generic types

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

Working with generic types

- [Instructor] Generic types solve a similar problem as generic functions. Generic types can work with any type and not only with a particular class, structure or enumeration. That's why the built-in Swift collection types, the array, the set and the dictionary, are implemented as generic collections. Now, imagine that we need the wrapper that can hold values of various types. Strings, integers, dates, you name it. This playground contains some pre-defined structures. You can open the project from the exercise files folder, chapter 3, 0303, begin. Without generics, we end up having a wrapper for each type. We've got a StringWrapper for string types, and IntWrapper for integers, and a DateWrapper for dates. The number of dedicated wrapper types increases with each new type that we need to support. Besides, with every added type, we duplicate code. The code becomes a maintenance nightmare, even choosing the right wrapper…

Contents