Reusable: No more String-typed instantiations on iOS Development
If your app has crashed because it couldn’t instantiate a nib
with name foo
, or because it couldn’t dequeue
a cell
named bar
, try this simple framework called Reusable.
When it comes to development, there are always pieces of code that are instantiated by using Strings. These pieces of codes are called String Literals
or String-typed
code. While they are very useful because they make the code easy to read and understand, they can be a huge problem for you later on.
Here are a few examples of String-typed code:
I know you’ve used or are using this code. In each example, you can see that there are String Literals in each instantiation. Though there’s nothing wrong with this code, it can definitely lead to a crash and should be improved.
These codes can be fixed by using the following:
Static constants
By placing static constants, re-typing String Literals can be avoided all over your project.
Enums with UIStoryboards
With storyboards, we can just simply create a convenience initializer and use an enum to store all storyboard constants, here’s an example:
While the above examples work, they can still be improved more, this is where the Reusable comes in, this library will help make cells, xib-based reusable views, and ViewControllers from Storyboards type-safe, and refactor proof.
How Reusable works is that you only have to implement a protocol on UITableViewCell
, UIView
, UIControl
, or UIViewController
classes. The protocols we’ll be implementing are using a concept called mixins, a protocol with a default implementation for all its methods.
Down below are a few examples of how Reusable is used.
UITableViewCell and UICollectionViewCells
UIViews
UIViewControllers
Conclusion
By using this framework not only will make your code safer, but it will also make it cleaner. Reusable has helped reduce crashes from my production applications, hopefully, it can help you too.
If you have any suggestions please write a comment below. 👇🏼