Xcode 尝试继承 UIContextMenuConfiguration 时出错

Xcode Error trying to subclass UIContextMenuConfiguration

我在尝试继承 UIContextMenuConfiguration 时遇到 Xcode 编译器错误。

下面是重现问题的简单代码:

@available(iOS 13.0, *)
class DateDifferenceContextMenu: UIContextMenuConfiguration {
    init(indexPath: IndexPath, dateDifference: Int) {
        super.init(identifier: nil, previewProvider: nil, actionProvider: nil)
    }
}

错误为:

Must call a designated initializer of the superclass 'UIContextMenuConfiguration'.

我的超级调用与指定的初始化程序匹配。 怎么了?

My super call matches the designated initializer.

不,它调用 convenience 初始化程序。

将您的代码更改为简单地调用 super.init(),或者使您的初始化程序成为 convenience 井。即:

class DateDifferenceContextMenu: UIContextMenuConfiguration {
    // use either this
    convenience init(indexPath: IndexPath, dateDifference: Int) {
        self.init(identifier: nil, previewProvider: nil, actionProvider: nil)
    }

    // or this
    init(indexPath: IndexPath, dateDifference: Int) {
        super.init()
    }

}