Using closures
class A {
var selectorClosure: (() -> Void)?
func invoke() {
self.selectorClosure?()
}
}
var a = A()
a.selectorClosure = { println("Selector called") }
a.invoke()
Note that this is nothing new, even in Obj-C the new APIs prefer using blocks over performSelector
(compare UIAlertView
which uses respondsToSelector:
and performSelector:
to call delegate methods, with the new UIAlertController
).
Using performSelector:
is always unsafe and doesn't play well with ARC (hence the ARC warnings for performSelector:
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…