22 Generics
So far, all the properties and functions you have written have worked on concrete types like Int, String, and Monster. You may have noticed, however, that Swift allows you to create arrays that contain any type at all. You can create arrays of built-in Swift types, like [Int] and [Double], as well as arrays of types you create, like [Monster] and [Person]. How is Array implemented? How can you write code that can work with a variety of types in the same way? The answer to both of these questions is “generics.”
Swift generics allow you to write types and functions that make use of types that are not yet known to you or the compiler. Many of the built-in types you have used throughout this book, including optionals, arrays, ...
Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.