0

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

StackGU
  • 868
  • 9
  • 22

1 Answers1

1

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?

finebel
  • 2,227
  • 1
  • 9
  • 20