Reputation: 3352
I am trying to set an initializer for a custom UICollectionViewCell class and I am receiving an error. I am new to Swift and understand that a model class must have a relevant initializer in order to use items from the class. Here is my code and the error I am receiving:
Upvotes: 0
Views: 220
Reputation: 73
if your 'Key' is not optional then you need to initialize the 'key' in Init().
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// do something here if you need ...
}
The decoder is useful if you want to restore your object to customer object.For example, if you store a class object in core data and read the record from core data then you need to do encode and decode for it.
Upvotes: 0
Reputation: 485
You need to set a default value to your key variable or make it Optional
var key: String?
Upvotes: 1
Reputation: 1499
you need to include the initializer for NSCoder:
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// do something here if you need ...
}
That should fix it for you.
Upvotes: 1