gpt4 book ai didi

ios - 不能继承 WKWebView

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:29 27 4
gpt4 key购买 nike

我正在尝试子类化 WKWebView。当我实现自己的初始化器时,出现了这个错误:

'required' initializer 'init(coder:)' must be provided by subclass of 'WKWebView'

好的,众所周知,我们必须为 UIView 的子类实现它。对于 UIView 的直接子类,它只需实现它就可以工作,但是对于 WKWebView 它似乎并不那么简单。我遵循了 Fix-it 提示,并将此片段添加到代码中:

required @availability(*, unavailable) convenience init!(coder: NSCoder!) {
fatalError("init(coder:) has not been implemented")
}

所以我得到了如下类:

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")
}
}

但是,当我这样做时,我会遇到另外四个错误:

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)。非常感谢。

最佳答案

像这样重写常规初始化。这对我有用,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")
}

关于ios - 不能继承 WKWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com