Swift: Overriding didSet results in a recursion

后端 未结 3 907
温柔的废话
温柔的废话 2021-02-07 18:41

When overriding the didSet observer of a property results in recursion, why?

class TwiceInt {
    var value:Int  = 0 {
        didSet {
            value *= 2
           


        
3条回答
  •  臣服心动
    2021-02-07 19:08

    it appears for some reason, despite the override it is still calling the superClass didSet.

    In the first example you end up in recursion because setting quad sets off the superclass didSet which in turn sets off the quads did set etc ect.

    In the second example setting the value causes both didSets to occur once each, then the quad didSet also sets of the super didSet on last time.

    quad.value = 5

    value =*2(superclass didSet) *4(subClass didSet) *2(superClass didSet) =80

提交回复
热议问题