Crashalot
Crashalot

Reputation: 34523

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

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
        }
    }
}

Upvotes: 3

Views: 6629

Answers (2)

Lumialxk
Lumialxk

Reputation: 6379

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.

Upvotes: 4

Satre
Satre

Reputation: 1744

I believe that the reason for your error is that UITableViewCell already defines a property called selected.

Upvotes: 2

Related Questions