Chapter 21. Generics

When we introduced optionals and collection types earlier in this book, we briefly showed you the “long-form” syntax for each type, like:

  • Optional<String>

  • Array<String>

  • Dictionary<String>

  • Set<String>

Most of these types have a preferred shorthand syntax, like String? or [String], and we did not really discuss the angle bracket syntax except to show you that it exists.

We also told you that, for example, Array<String> is not the same type as Array<Int>. And that is true – but the Swift standard library only defines one Array. So what is the deal with the Array<String> type and others like it? You are ready to find out.

Optional and the collection types like Array are what we call generic types, because they are designed to ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition 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.