我想将一个类的闭包动态附加到另一个方法上。例如,因为UIViewController
我想添加扩展名,以便可以在viewDidLoad
事件中插入代码。我尝试了类似下面的操作,但是不起作用:
class BaseViewController: UIViewController, MyProtocol {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Setup and bind
configure()
}
override func viewDidLoad() {
super.viewDidLoad()
// Call MyProtocol.myDidLoad but not explicitly!
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Call viewWillAppear but not explicitly!
}
}
protocol MyProtocol { }
extension MyProtocol where Self: UIViewController {
func configure() {
// Something like below isn't possible???
self.viewDidLoad += myDidLoad
self.viewWillAppear += myWillAppear
}
func myDidLoad() {
// Something
}
func myWillAppear() {
// Something
}
}
无需从中显式调用协议功能,是否可以实现这一点UIViewController
?我不想遍历所有类并为每个函数调用协议函数。那将是乏味的,冗余的代码,并且很容易被忽略。有没有更优雅的方式?
更新:
下面是我可以考虑到的限制的唯一方法,但是它使用的是对结构的继承,并且仍然需要我显式调用协议扩展功能,这不是我想要做的:
class FirstViewController: BaseViewController {
}
class BaseViewController: UIViewController, MyProtocol, MyProtocol2, MyProtocol3 {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure protocols
configure(self as MyProtocol)
configure(self as MyProtocol2)
configure(self as MyProtocol3)
}
override func viewDidLoad() {
super.viewDidLoad()
// Load protocols
viewDidLoad(self as MyProtocol)
viewDidLoad(self as MyProtocol2)
viewDidLoad(self as MyProtocol3)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Prerender protocols
viewWillAppear(self as MyProtocol)
viewWillAppear(self as MyProtocol2)
viewWillAppear(self as MyProtocol3)
}
}
protocol MyProtocol { }
extension MyProtocol where Self: UIViewController {
func configure(delegate: MyProtocol) {
print("MyProtocol.configure")
}
func viewDidLoad(delegate: MyProtocol) {
print("MyProtocol.viewDidLoad")
}
func viewWillAppear(delegate: MyProtocol) {
print("MyProtocol.viewWillAppear")
}
}
protocol MyProtocol2 { }
extension MyProtocol2 where Self: UIViewController {
func configure(delegate: MyProtocol2) {
print("MyProtocol2.configure")
}
func viewDidLoad(delegate: MyProtocol2) {
print("MyProtocol2.viewDidLoad")
}
func viewWillAppear(delegate: MyProtocol2) {
print("MyProtocol2.viewWillAppear")
}
}
protocol MyProtocol3 { }
extension MyProtocol3 where Self: UIViewController {
func configure(delegate: MyProtocol3) {
print("MyProtocol3.configure")
}
func viewDidLoad(delegate: MyProtocol3) {
print("MyProtocol3.viewDidLoad")
}
func viewWillAppear(delegate: MyProtocol3) {
print("MyProtocol3.viewWillAppear")
}
}
//Output:
//MyProtocol.configure
//MyProtocol2.configure
//MyProtocol3.configure
//MyProtocol.viewDidLoad
//MyProtocol2.viewDidLoad
//MyProtocol3.viewDidLoad
//MyProtocol.viewWillAppear
//MyProtocol2.viewWillAppear
//MyProtocol3.viewWillAppear
这破坏了面向协议编程的全部目的。在iOS开发范围内工作时,Swift是否可以处理一种优雅的方法,或者POP在这种情况下不起作用?
Swift和Objective-C都是单继承语言,因此您无法做任何事情。
扩展只能添加其他方法,不能覆盖方法。只有子类可以覆盖现有类中的方法。考虑多个扩展覆盖相同方法的情况-应该调用哪个方法?所有这些?如果是的话,按什么顺序?见钻石问题
最简单的方法是创建提供所需功能的UIViewController子类。尽管程序员可以创建一个符合协议但不对正确的超类进行子类化的类,但考虑到该类和协议是在同一行输入的,这将是一个非常愚蠢的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句