Ios 是否可以将逻辑动态附加到方法?
我想从类的init动态地将闭包附加到另一个方法。例如,对于UIViewController,我想添加一个扩展,以便在viewDidLoad事件中插入代码。我尝试了下面的方法,但不起作用:Ios 是否可以将逻辑动态附加到方法?,ios,swift,swift2,protocol-extension,Ios,Swift,Swift2,Protocol Extension,我想从类的init动态地将闭包附加到另一个方法。例如,对于UIViewController,我想添加一个扩展,以便在viewDidLoad事件中插入代码。我尝试了下面的方法,但不起作用: class BaseViewController: UIViewController, MyProtocol { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) // Setu
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开发的范围内,是否有一种斯威夫特可以处理的优雅方法,或者POP在这种情况下不起作用?您不可能做到这一点,因为斯威夫特和Objective-C都是单继承语言 扩展只能添加其他方法,不能重写方法。只有子类可以重写现有类中的方法。考虑多个扩展覆盖同一个方法(应该调用哪个方法)的情况?全部?如果是,顺序是什么?看
最简单的方法是创建提供所需功能的UIViewController子类。尽管程序员可以创建一个符合协议的类,但不能为正确的超类创建子类,考虑到类和协议是在同一行中输入的,这将是一个非常愚蠢的错误。您想要做的是不可能的,因为Swift和Objective-C都是单继承语言 扩展只能添加其他方法,不能重写方法。只有子类可以重写现有类中的方法。考虑多个扩展覆盖同一个方法(应该调用哪个方法)的情况?全部?如果是,顺序是什么?看
最简单的方法是创建提供所需功能的UIViewController子类。尽管程序员可以创建一个符合协议的类,但不将正确的超类作为子类,考虑到类和协议是在同一行中输入的,这将是一个非常愚蠢的错误。我按照您的建议做了,并用新代码更新了答案,这非常乏味,也不是很优雅。我猜POP不是为iOS开发而设计的?我认为更重要的是,虽然POP提供了多重继承的一些特性,但它与多重继承不同。Swift和Objective-C都是单继承语言。协议提供了多重继承的许多好处,但避免了菱形问题。即使是那些具有多重继承的语言也要求您显式地解决菱形问题,因此不允许执行ViewDidLoad的所有实现。我按照您的建议做了,并用新代码更新了答案,这非常繁琐,也不是很优雅。我猜POP不是为iOS开发而设计的?我认为更重要的是,虽然POP提供了多重继承的一些特性,但它与多重继承不同。Swift和Objective-C都是单继承语言。协议提供了多重继承的许多好处,但避免了菱形问题。即使是那些具有多重继承的语言也需要显式解决菱形问题,因此不允许执行viewDidLoad的所有实现