Extend Swift types with static parameters for reuse, consistency, and faster coding

I believe there comes a time when every developer adopts a technique for reusing common parameters. For years, I’ve been keeping all of my ”settings” in a single place i.e.

which I would use like:

  • label.textColor = GlobalSettings.darkRed

  • directionalLayoutMargins = GlobalSettings.defaultMargins

But there is a better way.

Extending individual types:

This makes coding so much easier and less verbose at the point of assigning - just start typing with a dot and rely on autocomplete to get to:

  • label.textColor = .darkRed

  • directionalLayoutMargins = .defaultMargins

And as a bonus, keeping these extensions in individual files reduces compile time. Remember GlobalSettings? Changing a single parameter in there would require every source file that references GlobalSettings to get recompiled.

--

Images above were generated with Snippet built by Seb Vidal.