StackGU
StackGU

Reputation: 958

UIView not initialised with model

I'm setting up a UIView with a cardModel in this way:

var cardModel : CardModel!{
    didSet{
        nameLabel.text = cardModel!.name
    }
}


init(frame: CGRect, cardModel: CardModel) {
    super.init(frame: frame)
    
    
    self.cardModel = cardModel
    
    setUpUI()
    
    setUpGestureRecognizers()
    
    setUpPlayer()
    
}

The problem is that the variable cardModel didSet is not called I actually don't know why

Upvotes: 0

Views: 32

Answers (1)

finebel
finebel

Reputation: 2355

didSet isn't called because you are initializing carModel in init. So to get it work, you could put the initialization of carModel inside a closure like so:

init(frame: CGRect, cardModel: CardModel) {
    super.init(frame: frame)
    
    ({ self.cardModel = cardModel })()
    
    //...
}

For further information: Is it possible to allow didSet to be called during initialization in Swift?

Upvotes: 1

Related Questions