How to create a custom getter method in swift 4

后端 未结 2 1952
遥遥无期
遥遥无期 2021-01-15 11:25

I am trying to create a custom setter method for my property. Below is my code.

I am getting a waring Attempting to access \'myProperty\' within its own

2条回答
  •  感情败类
    2021-01-15 11:59

    Create a backing ivar and add a custom setter:

    private _myProperty: String 
    var myProperty: String {
        get {
            if CONDITION1 {
                return CONDITION1_STRING
            } else if CONDITION2 {
                return CONDITION2_STRING
            } else {
                return _myProperty
            }
        }
        set {
            _myProperty = newValue
        }
    }
    

提交回复
热议问题