I will give you a very good Swifty example based on this post . Although it is a little more complicated.
Imagine that you have a project in which you have 15 collectionViews in your application. For each of them, you must set cellIdentifier and nibName. Are you sure you want to rewrite all the code for your 15 times?
There is a very POP solution in your problem:
Help yourself by writing a protocol that returns a string version of our ClassName
protocol ReusableView: class { static var defaultReuseIdentifier: String { get } } extension ReusableView where Self: UIView { static var defaultReuseIdentifier: String { return String(Self) } } extension BookCell : ReusableView{ }
The same goes for the nibName
each custom cell you created:
protocol NibLoadableView: class { static var nibName: String { get } } extension NibLoadableView where Self: UIView { static var nibName: String { return String(Self) } } extension BookCell: NibLoadableView { }
So now that I need nibName
, I would just do
BookCell.nibName
And where I need a cellIdentifier
, I would just do:
BookCell.defaultReuseIdentifier
Now specifically to your question. Do you think we need to change cellIdentifier to each new instance of BookCell ?! No! All BookCell cells will have the same identifier. This is not something that would change by one instance. The result was static
While I was answering your question, the decision to reduce the number of rows for 15 collectionViews can still be significantly improved, so see the blog post.
This blog post has actually been turned into a video by NatashaTheRobot
Honey source share