Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code.
Extensions in Swift can:
When to use Swift Extensions:
When not to use:
Simple example:
extension Bool {
public mutating func toggle() -> Bool {
self = !self
return self
}
}
var myBool: Bool = true
print(myBool.toggle()) // false