Initializer不会覆盖其超类中的指定初始值设定项 [英] Initializer does not override a designated initializer from its superclass
问题描述
所以我刚刚升级到Xcode 6.3 Beta 3,并且出现了很多与以下内容相关的错误:
So I've just upgraded to Xcode 6.3 Beta 3 and a lot of error(s) are appearing relating to the following:
初始化程序不会覆盖其超类中的指定初始值设定项。
Initializer does not override a designated initializer from its superclass.
override init() {
super.init()
}
例如这是 UIButton
class:
For example this is a UIButton
class:
class CustomButton: UIButton {
var target: AnyObject!
var selector: Selector!
var action: (() -> Void)!
override init() { // Initializer does not override a designated initializer from its superclass
super.init() // Must call a designated initializer of the superclass 'UIButton'
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
}
这是我的 UIViewController
类之一:
class CustomAlertView: UIViewController {
required init(coder aDecoder: NSCoder) {
fatalError("NSCoding not supported")
}
required override init() { // Initializer does not override a designated initializer from its superclass
super.init() // Must call a designated initializer of the superclass 'UIViewController'
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
}
推荐答案
我的解决方案是一个快速修复,但我认为比发布说明中Apple的目的更容易。有关更多信息,请搜索19775924 http://adcdownload.apple.com//Developer_Tools/Xcode_6。这里有3_beta_3 / Xcode_6.3_beta_3_Release_Notes.pdf 。 Apple说的是你创建一个Objective-C文件并对其进行扩展(必须将其添加到头文件和所有文件中)并且它在Xcode 6.3 beta 3中的已知问题中,所以我认为很容易做我做的事情:
My solution is a quick fix, but I think is easier than what Apple purposes on the the Release Notes. For more information search for 19775924 http://adcdownload.apple.com//Developer_Tools/Xcode_6.3_beta_3/Xcode_6.3_beta_3_Release_Notes.pdf here. What Apple says is that you create an Objective-C file and extend it (having to add it to the header files and all) and it's on "Known Issues in Xcode 6.3 beta 3", so I think is easy to do what I did:
这是我为 UIButton修复它的方式
:
class CustomButton : UIButton {
init() {
super.init(frame: CGRectZero)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这是我的一个ViewControllers(如果不需要则删除公共):
And this is one of my ViewControllers (remove public if not needed):
public class GenericViewController: UIViewController {
public init() {
super.init(nibName: nil, bundle: nil)
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我不喜欢使用IB所以我也有 UIView
,因为我将视图与 viewController
分开(如果不需要则删除public ):
I don't use IB so I also have UIView
, because I do separate the view from the viewController
(remove public if not needed):
public class GenericMenuView: UIView {
public init() {
super.init(frame: CGRectZero)
}
public required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我在视图中需要这个,因为我有 setupViews
我在init上调用的所有子类中重写的方法。并且使用AutoLayout我不需要任何帧(所以我不用frame参数覆盖init)。
I need this specially in views because I have a setupViews
method that I override in all subclasses that is called on the init. And using AutoLayout I don't need any frames (so I don't override the init with the frame parameter).
所以看来你必须放弃倍率
。哦!并且一定不要调用 self.init()
,否则该类永远不会被初始化(并且在一些内部超时后它会崩溃)。
So it seems you have to drop override
. Oh! and be sure to not call self.init()
or the class is never initialized (and it crashes after some internal timeout).
这篇关于Initializer不会覆盖其超类中的指定初始值设定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!