-
Notifications
You must be signed in to change notification settings - Fork 0
Extensions
Radu Ghitescu edited this page Nov 3, 2020
·
2 revisions
Extensions in Swift can:
- Add computed instance properties and computed type properties
- Define instance methods and type methods
- Provide new initializers
- Define subscripts
- Define and use new nested types
- Make an existing type conform to a protocol
Extensions affect all future and existing instances of the extended type.
Syntax:
extension SomeType {
// extends functionality of SomeType
}
extension SomeType: Protocol1, Protocol2 {
// extends SomeType with protocol conformance
}
extension Int {
var squared: Int {
self * self
}
}
print(10.squared)