my code contains both objective-c and swift classes and so far, everything worked pretty well. however, i added a custom gesture recognizer that inherits from UIGestureRecognizer. My code compiles but when I try to run it I get the following error:
我的代码包含objective-c和swift类,到目前为止,一切都运行良好。但是,我添加了一个继承自UIGestureRecognizer的自定义手势识别器。我的代码编译,但当我尝试运行它时,我收到以下错误:
fatal error: use of unimplemented initializer 'init(target:action:)' for class
I assume that this error originates in the mix of objective-c and swift code but I have no clue how to solve it (besides rewriting everything in one of the languages). Your help would be highly appreciated.
我认为这个错误起源于objective-c和swift代码的混合,但我不知道如何解决它(除了用其中一种语言重写所有内容)。非常感谢您的帮助。
2 个解决方案
#1
0
That's quite normal, as
那是很正常的
init(target target: AnyObject?,
action action: Selector)
is exposed as a designated initializer in the documentation. However, it's strange that in the UIKit public sources it's declared as follows:
在文档中作为指定的初始化程序公开。然而,奇怪的是,在UIKit公共资源中,它声明如下:
public init(target: AnyObject?, action: Selector) // designated initializer
Conclusion: even though it's not declared with the required
keyword, you must implement it for your subclass. It may simply call the super's init(target: AnyObject?, action: Selector)
. Good luck.
结论:即使没有使用required关键字声明它,也必须为子类实现它。它可以简单地调用super的init(target:AnyObject?,action:Selector)。祝你好运。
#2
0
Extending to what Arthur mentioned, this is how Apple has defined it for init(coder aDecoder: NSCoder)
making it required for subclasses to implement.
延伸到Arthur所提到的,这就是Apple为init定义它的方式(编码器aDecoder:NSCoder),这使得子类需要实现。
The NSCoding protocol requires that conforming types implement the required initializer init(coder:). Classes that adopt NSCoding directly must implement this method. Subclasses of classes that adopt NSCoding that have one or more custom initializers or any properties without initial values must also implement this method. Xcode provides the following fix-it to serve as a placeholder implementation:
NSCoding协议要求符合类型实现所需的初始化程序init(编码器:)。直接采用NSCoding的类必须实现此方法。采用具有一个或多个自定义初始值设定项的NSCoding或没有初始值的任何属性的类的子类也必须实现此方法。 Xcode提供以下修复程序 - 用作占位符实现:
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#1
0
That's quite normal, as
那是很正常的
init(target target: AnyObject?,
action action: Selector)
is exposed as a designated initializer in the documentation. However, it's strange that in the UIKit public sources it's declared as follows:
在文档中作为指定的初始化程序公开。然而,奇怪的是,在UIKit公共资源中,它声明如下:
public init(target: AnyObject?, action: Selector) // designated initializer
Conclusion: even though it's not declared with the required
keyword, you must implement it for your subclass. It may simply call the super's init(target: AnyObject?, action: Selector)
. Good luck.
结论:即使没有使用required关键字声明它,也必须为子类实现它。它可以简单地调用super的init(target:AnyObject?,action:Selector)。祝你好运。
#2
0
Extending to what Arthur mentioned, this is how Apple has defined it for init(coder aDecoder: NSCoder)
making it required for subclasses to implement.
延伸到Arthur所提到的,这就是Apple为init定义它的方式(编码器aDecoder:NSCoder),这使得子类需要实现。
The NSCoding protocol requires that conforming types implement the required initializer init(coder:). Classes that adopt NSCoding directly must implement this method. Subclasses of classes that adopt NSCoding that have one or more custom initializers or any properties without initial values must also implement this method. Xcode provides the following fix-it to serve as a placeholder implementation:
NSCoding协议要求符合类型实现所需的初始化程序init(编码器:)。直接采用NSCoding的类必须实现此方法。采用具有一个或多个自定义初始值设定项的NSCoding或没有初始值的任何属性的类的子类也必须实现此方法。 Xcode提供以下修复程序 - 用作占位符实现:
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}