无法子类化WKWebView [英] Cannot subclass WKWebView
问题描述
我正在尝试继承WKWebView.当我实现自己的初始化程序时,出现此错误:
I am trying to subclass WKWebView. When I implement my own initializer, I got this error:
'required' initializer 'init(coder:)' must be provided by subclass of 'WKWebView'
好吧,众所周知,我们必须为UIView的子类实现它.对于UIView的直接子类,它只是实现即可工作,但是对于WKWebView,它似乎并不那么简单.我遵循了Fix-it提示,并将此代码段添加到了代码中:
Ok, that is well known that we have to implement it for subclasses of UIView. For a direct subclass of UIView it works just implementing it, but with WKWebView it does not seem so simple. I followed the Fix-it hint, and this snippet is added to the code:
required @availability(*, unavailable) convenience init!(coder: NSCoder!) {
fatalError("init(coder:) has not been implemented")
}
所以我得到了一个类似于以下的类:
So I get a class like the following:
import WebKit
class TSWebView : WKWebView {
let s: String
let i: Int
init(s: String, i: Int) {
self.s = s
self.i = i
super.init(frame: CGRectZero, configuration: WKWebViewConfiguration())
}
required @availability(*, unavailable) convenience init!(coder: NSCoder!) {
fatalError("init(coder:) has not been implemented")
}
}
但是,当我这样做时,我又遇到了另外四个错误:
However, when I do this I get these four other errors:
expected declaration
required @availability(*, unavailable) convenience init!(coder: NSCoder!) {
consecutive declarations on a line must be separated by ';'
required @availability(*, unavailable) convenience init!(coder: NSCoder!) {
cannot override 'init' which has been marked unavailable
required @availability(*, unavailable) convenience init!(coder: NSCoder!) {
'required' modifier must be present on all overrides of a required initializer
required @availability(*, unavailable) convenience init!(coder: NSCoder!) {
有什么想法吗?我的Xcode版本是6.1.1(6A2008a).非常感谢.
Any ideas? My Xcode Version is 6.1.1 (6A2008a). Thanks a lot.
推荐答案
只需重写常规的初始化即可.这对我有效,Swift 5.
Just override the regular initialization like this. This worked for me, Swift 5.
override init(frame: CGRect, configuration: WKWebViewConfiguration) {
super.init(frame: frame, configuration: configuration)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
这篇关于无法子类化WKWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!