Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

swift - Build error when trying to override an initializer in Xcode 6.3 Beta 3

The following code shows build error in Xcode 6.3 Beta 3. The code works in Xcode 6.2 and Xcode 6.3 Beta 2.

class MyView: UIView {
  override init() {
    super.init()
    // Some init logic ...
  }

  override init(frame: CGRect) {
    super.init(frame: frame)
  }

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

Error message

initializer does not override a designated initializer from its superclass

Workaround?

There is a possible workaround of creating a protocol with init methods mentioned in Beta 3 release notes. I could not make it work both both init and init(frame: CGRect) initializers.

How can I fix those build errors?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A designated initializer of a subclass needs to call the designated initializer of Superclass. A convenience initializer can only call another convenience initializer or a designated initializer of that class.

init() is a convenience initializer for UIView, if you subclass UIView you should call its designated initializer which is init(frame: frame)

override init() {
super.init(frame: frame)
// Some init logic ...
}

EDIT: Apparently in Beta 3, UIView doesn't have convenience initializer called as init, so you need to remove the override keyword too, now this is a designated initializer so you need to call superclass's designated initializer

init() {
super.init(frame: frame)
// Some init logic ...
}

EDIT: Although this works but I think a better way to write this would be:

convenience init() {
self.init(frame:CGRectZero)
}

Source:Swift documentation

Rule 1 A designated initializer must call a designated initializer from its immediate superclass.

Rule 2 A convenience initializer must call another initializer from the same class.

Rule 3 A convenience initializer must ultimately call a designated initializer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...