Bag*_*yer 2 initialization ios reactive-cocoa swift uialertaction
这是我的代码:
class CustomAlertAction: UIAlertAction {
init(title : String) {
super.init(title: title, style: UIAlertActionStyle.Default) { (action) -> Void in
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了以下编译错误:
必须调用超类'UIAlertAction'的指定初始值设定项
我知道UIAlertAction的指定初始化程序是init().但是UIAlert的init(标题,样式,处理程序)不会调用它的指定初始化程序init()?
任何的想法?谢谢
PS:基于Apple的文档:
指定的初始化程序必须从其直接超类中调用指定的初始化程序."
这是否意味着不允许在Swift中继承UIAlertAction?在Objective-C中这样做没有问题.
我想创建UIAlertAction的子类的原因是因为我想添加一个ReactiveCocoa命令作为一个动作.
实际上,解决方案几乎肯定会使用类扩展.
extension UIAlertAction {
convenience init(title: String) {
self.init(title: title, style: .Default, handler: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
let okayAction = UIAlertAction(title: "Okay")
Run Code Online (Sandbox Code Playgroud)
您仍然可以子类化以UIAlertAction向其添加属性.子类仍然可以使用您在UIAlertAction课堂上扩展的这个convience初始化程序.
最后,为了它的价值,有人已经创建了一个Reactive Cocoa UIAlertAction子类.他们只是在Objective-C中做到了.看看将Objective-C添加到项目中没有什么害处,你可以采用这种方法......或者只是安装那个pod ...