What are extensions and when to use them

suggest change

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

Source

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents