Reusability in Swift, SimpleDataSource use-case demo

Ivan Rep
Ivan Rep
Nov 8, 2018 · 2 min read

Displaying a list of items is one the first skills that every iOS developer learns. We are thought how to implement a data source, and how to dequeue and setup cells. Yet, the standard approach has a couple of shortcomings. The main one for me being that it’s very hard to reuse.

To solve this problem I’ve created the SimpleDataSource framework. By using a data driven approach and reorganising responsibilities, the framework significantly decreases the amount of boilerplate code and improves reusability.

Following post showcases a simple use-case implemented using the framework.

Note: For simplicity I’ll be addressing UITableView only, but everything, including framework support, extends to UICollectionView.

For our data I chose some of the best movies ever released, most of which have a one thing in common, Steven Seagal. Along with the movies we will be presenting a short cast list.

Let’s setup the cells, models and data we will need. This step isn’t affected by our framework choice.

Instead of implementing a custom UITableViewDataSource, we will use SimpleTableViewDataSource. We simply initialise and set it as the tableViews dataSource.

Our data source knows how to do a single thing, present [[AnyDequeuableCollectionViewCellViewModel]].

AnyDequeuableCollectionViewCellViewModel is a type-erased model that knows how to register and dequeue a single cell.

We won’t create the model by hand, but will instead allow Swift’s strong type system to do it for us.

Next, we need to implement PresentingTableViewCell protocol in our cells. It requires a single function whose parameter is the protocols associated type. For now ignore the type constraint.

present(viewModel:) function should apply the ViewModel parameter to the cell and optionally perform any additional cell setup.

Similarly our models need to implement the DequeuableTableViewCellViewModel protocol. The protocol defines a TableViewCell associated type constrained to the previously defined PresentingTableViewCell, whose associated type is in turn constrained to DequeuableTableViewCellViewModel.

Although seemingly not as simple as the previous one, the default implementations allow us to specify only the associated type, as long as TableViewCell.ViewModel is equal to the implementing entity.

Now to create the AnyDequeuableCollectionViewCellViewModel simply use tableViewPresentable computed property available on Actor and Movie models, and present the created models on our data source.

To found out how it works in the background, take a look at the framework.

Thank you for reading! Feel free to comment and share.

Ivan Rep

Written by

Ivan Rep

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade