What if you could add new superpowers to your code without ever touching its original heart?
Why extensions add capability without modifying in Swift - The Real Reasons
Imagine you have a favorite recipe book, but you want to add new recipes without rewriting or damaging the original pages.
Manually copying and changing the recipe book is slow and risky. You might lose original recipes or make mistakes that ruin the book.
Extensions let you add new recipes on separate pages that attach to the book, so the original stays safe and you get new dishes easily.
class Dog { func bark() { print("Woof") } } // To add a new trick, you must change Dog class directly
extension Dog { func sit() { print("Sitting") } }
// Adds new behavior without touching original Dog codeExtensions let you grow your code's abilities safely and flexibly, like adding new tools without breaking old ones.
You have a basic app for photos, and later want to add filters. Extensions let you add filter functions without changing the original photo code.
Extensions add new features without changing original code.
They keep code safe and easy to maintain.
They help your programs grow step-by-step like adding new pages to a book.