Using Swift, is it possible to test if an object implements an optional protocol method without actually calling that method? This works except for cases where the optional methods differ only by their signature.
Consider this code...
@objc public protocol TestDelegate : AnyObject {
@objc optional func testx()
@objc optional func test(with string:String)
@objc optional func test(with2 int:Int)
}
let delegate:TestDelegate? = nil
if let _ = delegate?.test(with:) {
print("supports 'test(with:)'")
}
if let _ = delegate?.testx {
print("supports 'testx'")
}
If you paste the above in a playground, it works as expected.
However, if you change testx
to test
, it no longer works.
Likewise, if you change test(with2)
to test(with)
then that won't work either.
Is there any way to test for those methods that only differ by signature?