From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Decorator via Swift extensions

Decorator via Swift extensions - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Decorator via Swift extensions

- [Instructor] Swift type extensions are another option to implement the decorator pattern. Extensions can add new methods and computed properties to a type without changing their source code. Note however that you can't override existing functionality in an extension. Also extensions can't add stored properties or property observers to existing properties. Implement the object record instead if you need to replace existing functionality or add stored properties to a class. But if you want to add new methods or completed properties to a type, swift extensions are the way to go. Let me prove it. You can follow along with me by opening the playground project from the exercise files folder chapter six, six three begin. We start by adding a new playground page. I call it decorator via extension. I remove the boiler plate code, we only need to import the foundational framework. Next we create a user default extension. Extension user defaults and we'll implement the set date for key and the…

Contents