Karl Bowden

View Routing Concepts in Swift

Back story of view routing in Swift. All code is conceptual and intended to illustrate concepts only.

It’s uncommon to see a backend web framework without a router. It makes sense. The web is built around URL’s that naturally branch out to routes.

Since the rise of frontend frameworks, routers have become a natural fit there too. Plus there is no shortage of choice for routers.

The function of a UI router is fairly simple:

That really is all there is to it, and it’s something we have been doing for a long time with coordinator and presenter patterns. The router is essentially a set of helper functions for declaring these patterns.

It is especially helpful when combined with state and side-effect separation. With very specific advantages:

At this point you can probably guess the frontend web frameworks that implement these patterns (in part or whole): Elm, Cycle, Redux, React, Angular, Vue, etc. Familiarity with these frameworks is not necessary though, as we are concerned more with how to apply the concepts behind them in our swift applications.

Introducing Featherweight Router

The main main downside to implementing routers in iOS has been the amount of boilerplate code it requires. I have repeated the pattern often enough that it warranted moving into a project of it’s own. Hence I introduce to you the Featherweight Router project. It was designed to be:

Featherweight Router doesn’t make any decisions for you outside of declaring a route structure. You still need to choose a UI framework, how to handle route changes and where to store state.

Wrapping up

Want to learn more? Check out:

My sources of inspiration:

With a special thank you to every person promoting the use of solid functional patterns.