fuz*_*uzz 37 initializer uiviewcontroller ios swift
所以我刚刚升级到Xcode 6.3 Beta 3,并且出现了很多与以下内容相关的错误:
Initializer不会覆盖其超类中的指定初始值设定项.
override init() {
super.init()
}
Run Code Online (Sandbox Code Playgroud)
例如,这是一个UIButton类:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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)
}
}
Run Code Online (Sandbox Code Playgroud)
Llu*_*ard 44
我的解决方案是一个快速解决方案,但我认为比发布说明中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中的已知问题"中,所以我认为很容易做我做的事情:
这就是我修复它的方法UIButton:
class CustomButton : UIButton {
init() {
super.init(frame: CGRectZero)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的一个ViewControllers(如果不需要,删除公共):
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")
}
}
Run Code Online (Sandbox Code Playgroud)
我不使用IB,所以我也有UIView,因为我将视图与viewController(如果不需要删除公共)分开:
public class GenericMenuView: UIView {
public init() {
super.init(frame: CGRectZero)
}
public required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Run Code Online (Sandbox Code Playgroud)
我在视图中特别需要这个,因为我有一个setupViews方法可以覆盖在init上调用的所有子类.使用AutoLayout我不需要任何帧(所以我不用frame参数覆盖init).
所以看来你必须放弃override.哦! 并确保不会调用self.init()或类永远不会初始化(并在内部超时后崩溃).
根据此处的Apple 文档,您要覆盖的是便利初始化程序。因此,为了使初始化程序正常工作,您必须将方法更改为
override convenience init() {
super.init()
}
Run Code Online (Sandbox Code Playgroud)
您可以这样做,或者如果您除了调用超类初始值设定项之外并未真正使用它,则可以删除它。
| 归档时间: |
|
| 查看次数: |
43746 次 |
| 最近记录: |