I was writing some code for a UICollectionViewCell
class and I normally include this initialiser when I create a new subclass for UICollectionViewCell
, and saw that frequently this is paired with a required init?(coder: NSCoder)
initialiser, so I was wondering if super.init(frame: CGRect)
is the designated initialiser for the UICollectionViewCell
class?
Is super.init(frame: CGRect) the designated initialiser for a UICollectionViewCell class?
According to the documentation regarding UIView
, the init(frame: CGRect)
is the designated initialiser for UIView
when I instantiate a UIView
object programatically. Additional, there's an article on Medium by Abdul Ahad titled "Different types of Init in Swift" (I can't link to this article here unfortunately as the platform rules prohibit it), goes into detail about the various types of initialisers and explains that init(frame: CGRect)
is also the designated initialiser for UICollectionViewCell
. required init?(coder: NSCoder)
on the other hand creates a view from data in an unarchiver. And according to "Passing data by using -required init decoder" also on Medium, we use the decoder in this instance to tell Xcode that we aren't relying on storyboard to instantiate the view (by implementing a fatalError("not using storyboard")
) message inside the init?(coder:)
method.