Swift 4.2 Setter Getter, All paths through this function will call itself Swift 4.2 Setter Getter, All paths through this function will call itself swift swift

Swift 4.2 Setter Getter, All paths through this function will call itself


Your issue is that there is no stored property type for the getter to return. type is a computed property. When you try to read its value, it calls the getter you defined. This getter calls the getter, which in turn calls the getter which calls the getter... and so on. You have infinite recursion.

Most likely, what you meant to do is have a stored property, that just has some fancy behaviour whenever its set. Rather than using a computed property with a custom get and set, use a stored property with a willSet or didSet block:

@objc var type: DecisionType {    didSet {        let isDecisionDouble = newValue == DecisionType.DecisionDouble        okButton.isHidden = isDecisionDouble;        yesButton.isHidden = !isDecisionDouble;        noButton.isHidden = !isDecisionDouble;    }}


A better approach for this case if to have one extra property which you will use it in return value for getter and you set it when your main property change. For instance let say your main property which you use is type then have an extra property _type note the underscore next to it. Then here is how you would set and retrieve your and set your main property type

// This is a an extra property which you will use internallyprivate var _type: DecisionType?// Then use it as shown belowvar type:DecisionType? {get {return _type}set {  _type = newValue}}


My issue was that I had get/set in the parent protocol extension rather than the protocol extension of the actual protocol:

protocol MainProtocol: SubProtocol {   var mainVariable: String { get set } }protocol SubProtocol {    var subVariable: String { get set } }extension MainProtocol {    var mainVariable { get { return self.mainVariable } set { self.mainVariable = newValue } }    //I KEPT MY VARIABLE HERE.      var subVariable { get { return self.subVariable } set { self.subVariable = newValue } }}extension SubProtocol {    //THIS IS WHERE THE VARIABLE SHOULD HAVE BEEN}