11.7 Swift Class Extensions
Another way to add new functionality to a Swift class is to use an extension. Extensions can be used to add features such as methods, initializers, computed properties and subscripts to an existing class without the need to create and reference a subclass. This is particularly powerful when using extensions to add functionality to the built-in classes of the Swift language and iOS SDK frameworks.
A class is extended using the following syntax:
extension ClassName {
// new features here
}
For the purposes of an example, assume that we need to add some additional properties to the standard Double class that will return the value raised to the power 2 and 3. This functionality can be added using the following extension declaration:
extension Double {
var squared: Double {
return self * self
}
...