Sunday, April 3, 2016

Protocol-Oriented MVVM

Natasha Murashev:

Instead of having that huge configure method, you can take out every part of it and put it into a SwitchWithTextCellProtocol for that cell. That got me really excited. This way, I can have my view model conform to this protocol, and set all these properties right there. Now, I don’t have to have this huge configure method, but I still have a way to ensure that every single property is actually set.

[…]

With protocol extensions in Swift 2.0, you can do the same thing with default values. If there is a certain color that’s relevant for most cells, you can just extend it and set that color. Any view model that implements this doesn’t have to set the color anymore.

[…]

Instead of tying my protocols to the actual cell, I can tie it to a more generic TextPresentable. This way, any view that has a label, not just cells, can conform to the text presentable protocol.

Comments RSS · Twitter

Leave a Comment