Swift: How To Name Your Extensions

One of my favorite patterns in iOS programming with Swift is to create multiple extensions throughout my files to keep related methods together. For example, every time my ViewController conforms to a protocol, I keep the protocol methods together in an extension. Same goes for multiple private styling methods or private cell configuration methods in a table view, etc.

The only thing missing from this pattern was that I was unable to name the extensions. Instead, I had to use // MARK: everywhere to keep track of the groupings. I mentioned this to @allonsykraken the other day, and he showed me a simple trick for this – using typealias!

I’m sure you’re now wondering the first thought that popped into my head when I saw this – what about the groupings in the Xcode nav bar?!!! Don’t worry, it’s all there!

Extensions Swift

Let me know what you think about naming your extensions like this in the comments! Would love feedback as I’m still deciding how much I like this vs just using MARK: here.

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.