My thoughts on - StatefulViewController
I had a chance to play a little bit with a nice pod that is called StatefulViewController, created by Alexander Schuch. StatefulViewController works with both
UIViewController, and allows you to introduce placeholders for their different states: Loading, Error, Empty or Content. Everything is based on an intuitive protocol and after providing your placeholder views and implementing required methods everything works like a charm. You can find this pod on Alexander’s github. I’ve looked into the code of this pod and I found a few things that I want to share with you today.
Whole idea is based on ViewStateMachine, which handles transitions of views that you have assigned to each state. All states are represented by this nice enum
Notice, that this enum conforms to
Equatable protocol, which lets us implement
"==" operator for this enum. When you do this, standard library provides an implementation for
"!=". This move allows us to compare states later on.
Another thing that was interesting for me was
subscript provided for a
ViewStateMachine class.”You use subscripts to set and retrieve values by index without needing separate methods for setting and retrieval.” - note from Apple.
###Extension property Normally, you can’t add a property to a class via an extension, but you can achive this using an associated object.
I really liked the documentation that was provided with this pod. In Objective-C we had a nice header file, which was pretty easy to read from. Here we have two files.
StatefulViewController.swift file, which provides nice documentation and
StatefulViewControllerImplementation.swift which containts implementation of
It was really interesting code, which taught me some new thing. I hope that you liked it too!
This article is cross-posted with my my personal blog.