Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computed property generates "Overriding declaration requires an 'override' keyword" error

Tags:

ios

swift

Code below. Why does the "Overriding declaration requires an 'override' keyword" error occur? Isn't this the "Swift" way of providing getters and setters to properties?

class TestClass : UICollectionViewCell {
        var _selected = false

        var selected : Bool {
        get {
            return _selected
        }

        set {
            _selected = newValue
            selectedView.hidden = !_selected
        }
    }
}
like image 847
Crashalot Avatar asked Jan 02 '26 06:01

Crashalot


1 Answers

First,don't use _selected in Swift.It's Objective-C style. Second, and override keyword before var selected : Bool because its super has declared a property with the same name.

Update: This is a store value,you don't need to do anything to get its value because swift will store it automaticlly.

   override var selected : Bool {
    get {
      return super.selected
    }
    set {
        selectedView.hidden = !newValue
    }
    }

Or use didSet:

   override var selected : Bool {
    didSet {
        selectedView.hidden = !selected
    }
    }

Use store value to keep a persistence value.Use computing(which has a getter) to get real-time value.

like image 191
Lumialxk Avatar answered Jan 04 '26 04:01

Lumialxk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!