For most applications of classes, properties are associated with each instance of the class. Using the Processor() class example
from earlier, each microprocessor has a different name, differ‐ ent numbers of registers, and potentially different widths for their data and address paths. Each instance of the class requires its own set of these property values. Properties used in this way are called instance properties—there are unique copies associ‐ ated with each instance of the class.
Some applications only require a single instance of a given property for the entire class. Consider a class that records employee data and must keep a record of the next available ID for a new employee. This ID should not be stored in each
instance, but it does need to be associated with the class in some way.
For such purposes, Swift provides type properties, which are properties associated with the class, not with a specific instance of the class. The same feature is referred to generically as a class variable, or in C++, Java, and C#, as a static member variable.
NOTE
Swift supports computed type properties in classes, but it does not currently support stored type properties in classes. Both are available in structures and enumerations. If you try to create a stored type property in a class, the compiler (as of the version of Xcode 7 available when this edition was updated) will give the error “class stored properties not yet supported in classes; did you mean static?” This would seem to imply that support may be coming in a future release.
To further complicate things, static stored properties are
supported in classes and provide similar functionality. They are discussed next.
Static properties.
In most cases where you would want to use the (unimplemented) stored type property in a class, you can use a static property instead. To create a static property, precede the property’s definition with the keyword static, as in the fol‐lowing example:
class Employee
{
static var nextID = 1
var familyName = "" var givenName = "" var employeeID = 0
In this class, only a single instance of the property nextID
exists, regardless of how many employee instances are cre‐ ated. To access a static property use dot-syntax with the class name, as in this example:
var emp = Employee()
emp.employeeID = Employee.nextID++
Note the following points when using static properties: • Static properties must be assigned a default value as
part of the class definition (the initialization routines, described later, only get called when instances are cre‐ ated).
• Static properties are inherited and shared by their sub‐ classes. For example, if a new class is defined that is a subclass of the Employee class, it will inherit and share the
same single instance of the nextID property.
Computed type properties.
To declare a computed type property for a class, precede the type property’s definition with the key‐ word class. The syntax for creating a read/write computedtype property is as follows:
class SomeClass {
class var someComputedProperty: SomeType {
get { return SomeType } set(valueName) {
// do something with valueName // that sets the property }
} }
The get method performs some operation to produce a value
of the declared type, and then returns it.
The set method performs some operation to initialize things
associated with the class—for example, it could modify a static property, and/or call system functions that have some interac‐
tion with the class. valueName is optional. It is used to refer to
the value passed into the function. If you omit it, you can refer to the value with the default name newValue.
If you want a read-only computed type property, omit the set
definition. In this case, you can also omit the get keyword and
reduce the var body to just the code that calculates and returns
the desired value.
The next example reimplements the Employee class from earlier
to show a computed type property in action. The _nextID static
variable is still present, but it is now protected from external access (see “Access Control” on page 151) and a computed type property is implemented to provide gated access to that static value.
class BetterEmployee
{
private static var _nextID = 1
class var nextID: Int
{
get { return _nextID } set { _nextID = newValue } }
var familyName = "" var givenName = "" var employeeID = 0
}
To access the computed type property, use dot-syntax with the class name, as before:
var be = BetterEmployee()
be.employeeID = BetterEmployee.nextID++
Even though this code appears to be the same as the earlier ver‐ sion, access to the static _nextID property is now only available
indirectly through the nextID computed type property’s get
and set methods, which are called automatically when the
Note the following when using computed type properties: • A computed type property cannot access instance prop‐
erties or instance methods because it is not called in rela‐ tion to an instance, but it can call type methods (see
“Type methods” on page 122) and other computed type properties, and it can access static properties in the class. • Computed type properties can be overridden in sub‐
classes. See “Overriding Superclass Entities” on page 126
for more information.