Problem with overridden properties in Swift

I have a subclass UIButtonas follows:

class VectorizedButton: UIButton {

    override var highlighted: Bool {
        didSet {
            setNeedsDisplay()
        }
    }

}

Everything works fine until I added this line to my root controller:

var twitterButton: TwitterButton?

TwitterButtonthe extends VectorizedButton.

Here is the error I get:

... UIView + Vectorized.swift: 42: 7: Class 'VectorizedButton' has no initializers ... UIView + Vectorized.swift: 44: 18: the stored property is "highlighted" without an initial value prevents synthesized initializers

Just set the default value:

override var highlighted: Bool = false

More severe error:

<unknown>:0: error: super.init called multiple times in initializer
<unknown>:0: error: super.init called multiple times in initializer
<unknown>:0: error: super.init called multiple times in initializer

Try to override init?

required init(coder aDecoder: NSCoder) {
    highlighted = false
    super.init(coder: aDecoder)
}

Even more mistakes:

error: 'self' used before super.init call
    highlighted = false
                ^
error: 'self' used before super.init call
    highlighted = false
    ^

Can anyone explain what is going on here?

+4
source share
1

init

:

class VectorizedButton: UIButton {

  override var highlighted: Bool {
    didSet {
        setNeedsDisplay()
    }
  }

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

}

class TwitterButton: VectorizedButton {

}

ViewController:

var twitterButton: TwitterButton?

, .

, init .

:

override var highlighted: Bool = false

init, :

override var highlighted: Bool {

    get { return false }
    set { super.highlighted = newValue }
}

:

required init(coder aDecoder: NSCoder) {
  highlighted = false
  super.init(coder: aDecoder)
}

super.init.

+2

All Articles