Creating an array of different types of objects

Ordinarily, it is not possible to create an array of different object types. However, since Burger, Fries, and Sauce all conform to CalorieCountProtocol, we can make an array that contains them:

  1. Type in the following code after all of the protocol and object declarations and run it. Since all of the food items conform to the CalorieCountProtocol protocol, you can add them to foodArray.
// create instances of each typelet burger = Burger()let fries = Fries()let sauce = Sauce.tomato// print the descriptionsprint(burger.description())print(fries.description())print(sauce.description())// since all items have adopted the same protocol, you can add them   to a single arraylet foodArray : [CalorieCountProtocol] ...

Get iOS 13 Programming for Beginners - Fourth 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.