Override a setter in swift

后端 未结 1 814
夕颜
夕颜 2020-12-12 02:05

I\'ve got a strange problem with setter in swift. I\'ve got class PlayingCard with code:

var rank: NSInteger {
    get{
        return self.rank
    }
    se         


        
相关标签:
1条回答
  • 2020-12-12 02:27

    By writing this...

    set(suit) {
        self.suit = suit
    }
    

    ... you introduce an infinite loop, because you call the setter from within the setter.

    If your property isn't computed, you should take advantage of willSet and didSet notifiers to perform any additional work before/after the property changes.

    By the way, you should remove te getter as well. It will cause another infinite loop when accessing the property.

    0 讨论(0)
提交回复
热议问题