Generics power arrays and dictionaries, JSON decoding, Combine publishers and many other parts of Swift and iOS.

4.1 Getting started with generics

func replaceNilValues<T>(from array: [T?], with element: T) -> [T] {
  array.compactMap {
    $0 == nil ? element : $0
  }
}
func max<T: Comparable>(lhs: T, rhs: T) -> T {
  return lhs > rhs ? lhs : rhs
}

Generic types

Protocols with associated types

Extending generics

Self and meta-types

4.2 Creating a generic networking library