I\'m trying to add a on-tap functionality to all UIViewControllers where they conform to protocol MyProtocol
.
Below is how i\'m doing it:
Matt's answer is correct. You can play in your playground to see exactly how it works. In next snippet you can to see that even though some object can perform objc selector, if not properly used, it could crash your code :-)
//: Playground - noun: a place where people can play
import Foundation
protocol Bar {}
extension Bar {
func bar()->Self {
print("bar from protocol Bar extension")
dump(self)
return self
}
}
class C: NSObject {
func dummy() {
print("dummy from C")
dump(self)
}
}
extension NSObject {
func foo()->NSObject {
print("foo from NSObject extension")
dump(self)
return self
}
}
let c = C()
c.respondsToSelector("foo") // true
c.respondsToSelector("dummy") // true
c.foo()
let i = c.performSelector("foo")// prints the same and DON'T crash
c.dummy() // works
//c.performSelector("dummy") // if uncommented then
/* prints as expected
dummy from C
▿ C #0
- super: <__lldb_expr_517.C: 0x7fcca0c05a60>
*/
// and crash!!! because performSelector returns Unmanaged<AnyObject>!
// and this is not what we return from dummy ( Void )
let o = NSObject()
o.foo()
extension NSObject: Bar {}
// try to uncomment this extension and see which version is called in following lines
/*
extension NSObject {
func bar() -> Self {
print("bar implemented in NSObject extension")
return self
}
}
*/
c.bar() // bar protocol extension Bar is called here
o.bar() // and here
o.respondsToSelector("foo") // true
o.respondsToSelector("bar") // false as expected ( see matt's answer !! )
o.performSelector("foo") // returns Unmanaged<AnyObject>(_value: <NSObject: 0x7fc40a541270>)
There is a big gotcha with performSelector: and friends: it can only be used with arguments and return values that are objects! So if you have something that takes e.g. a boolean or return void you're out of luck.
I would like to suggest further reading for everybody who use mixture of Swift and ObjectiveC
The problem is that Objective-C knows nothing of protocol extensions. Thus, you cannot use a protocol extension to inject a method into a class in such a way that Objective-C's messaging mechanism can see it. You need to declare bar
in the view controller class itself.
(I realize that this is precisely what you were trying to avoid doing, but I can't help that.)
A sort of workaround might be this:
override func viewDidLoad() {
super.viewDidLoad()
foo = "testing"
let tapGesture = UITapGestureRecognizer(target: self, action: "baz")
self.view.addGestureRecognizer(tapGesture)
}
func baz() {
self.bar()
}
We are now using Objective-C's messaging mechanism to call baz
and then Swift's messaging mechanism to call bar
, and of course that works. I realize it isn't as clean as what you had in mind, but at least now the implementation of bar
can live in the protocol extension.
(Of course, another solution would be to do what we did before protocol extensions existed: make all your view controllers inherit from some common custom UIViewController subclass containing bar
.)