Type Properties
Up to now, you have been working with instance properties. When you create a new instance of a type, that instance gets its own properties that are distinct from other instances of that type. Instance properties are useful for storing and computing values on an instance of a type.
However, when you need to store a value that is common to all instances of a type, it is wasteful to store a copy of that value in memory for every instance.
Instead, such properties can exist once, on the type itself, and are called type properties.
For example, all instances of a Square
type will have exactly four sides, so the number of sides for Square
might be stored in a type property.
If you create 1,000 instances of Square
in your program, ...
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.