tccpg288
tccpg288

Reputation: 3352

Unable to Set Initializer for Custom UICollectionView Cell Class

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:

enter image description here

Upvotes: 0

Views: 220

Answers (3)

Kay Weng
Kay Weng

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

carlos21
carlos21

Reputation: 485

You need to set a default value to your key variable or make it Optional

var key: String?

Upvotes: 1

CosmicMind
CosmicMind

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

Related Questions