【发布时间】:2021-11-18 21:18:22
【问题描述】:
我想根据 iOS 版本使用一个或另一个初始化器。如果不是 iOS 14,我想设置 action 以供以后使用。我对超级构造函数的调用发生在if/else:
class Control: UIControl {
var action: (() -> Void)?
init(action: @escaping () -> Void) {
self.action = action
if #available(iOS 14.0, *) {
let primaryAction = UIAction(handler: { _ in action() })
super.init(frame: .zero, primaryAction: primaryAction)
} else {
super.init(frame: .zero)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
电话是:
let controlThatDoesNotWork = Control(action: { print("this doesn not work") })
构建错误是
error: must call a designated initializer of the superclass 'UIControl'
super.init(frame: .zero, primaryAction: primaryAction)
^
知道我需要如何调用这个便利初始化程序以便构建它吗?
【问题讨论】:
标签: swift xcode ios14 uicontrol