Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override get-only property in swift

Tags:

swift

I have a property declared in parent class:

var textLabel: UILabel! { get }

Is it possible to make it writable in subclass?

like image 317
orkenstein Avatar asked Dec 04 '22 02:12

orkenstein


1 Answers

Yes it is possible to make it writable in a subclass. However, since it is a computed property, you will more than likely have to add another stored property to hold the new value you are assigning. I used strings to illustrate below:

class Parent {
    var text: String {
        get {
            return "Parent"
        }
    }
}

class Child: Parent {
    var _text: String = "Child"
    override var text: String {
        get {
            return _text
        }
        set {
            self._text = newValue
        }
    }
}

let child = Child()
child.text = "New String"
like image 126
Mr Beardsley Avatar answered Apr 19 '23 05:04

Mr Beardsley