The performSelector
family of methods are not available in Swift. So how can you call a method on an @objc
object, where the method to be called is chosen at runtime, and not known at compile time? NSInvocation
is apparently also not available in Swift.
I know that in Swift, you can send any method (for which there is an @objc
method declaration visible) to the type AnyObject
, similar to id
in Objective-C. However, that still requires you to hard-code the method name at compile-time. Is there a way to dynamically choose it at runtime?
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:
).
As of Xcode 7, the full family of performSelector methods are available in Swift, including performSelectorOnMainThread()
and performSelectorInBackground()
. Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With