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:
Which I would use like:
label.textColor = GlobalSettings.darkReddirectionalLayoutMargins = 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 = .darkReddirectionalLayoutMargins = .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.
← All development articles