Swift UIKit: How to register and dequeue reusable cells in more elegant way. Introducing Powerful Extension
No matter what programming language you use, you should avoid hardcoded values. It’s a bad process that can make bugs very easily. Literal Strings are the worst of all kinds. You can waste your precious time because of tiny typo mistakes.
Common Problem
If you are using UIKit, you probably have seen this code. Since you are a true developer, you will feel uncomfortable as soon as you see “MyCell”. It looks like legitimate code, and in fact it’s likely not causing any major problems. But there’s a better way, I’ll show you.
Better Approach with Generic
It’s by convention to use the same file name with the same reuse identifier. With that in mind, using Generic and String(describing: type)
, which returns Class Name, you can use this extension to write more robust code.
Conclusion
- Avoid Hard Coded Values
- Use extensions I just introduced!