Swift correct use of getters and setters

前端 未结 3 1406
野的像风
野的像风 2020-12-11 03:19

Can someone please help me understand the correct use of getters and setters in swift. I get the impression its not the same as say Java.

Is this the correct usage i

3条回答
  •  伪装坚强ぢ
    2020-12-11 04:01

    This is how setter and getter works as in Java:

    class Person {
        private var _name
        private var _age
        // .... other properties
    
        var name: String {
            get {
                return _name
            }
            set {
                _name = newValue
            }
        }
    
        var age: String {
            get {
                return _age
            }
            set {
                _age = newValue
            }
        }
    
    }
    

提交回复
热议问题