Swift how to avoid override init?(coder) in subclass

Viewed 514

A common annoyance with using Swift is that subclasses must implement init?(coder) even if you don't use Storyboard.

required init?(coder aDecoder: NSCoder) {
  fatalError("init(coder:) has not been implemented")
}

It's not elegant. How can I avoid overwriting it in a subclass?

1 Answers

If you have a base class, override it in the base class and add the @available(*, unavailable) flag so that its subclasses can avoid overriding the method, and this method also removed from code completion.

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
  fatalError("init(coder:) has not been implemented")
}
Related