Swift 2, Extensions and Call Answers

I'm not sure, it seems to me that this is some kind of error or poor implementation with protocol extensions in Swift 2.0.

I have protocol A, protocol b extending protocol A, and implementing methods in extending protocol B.

I matched the class instance to match protocol B, however, when checking with the responsesToSelector method for protocolA / B methods, the results were false.

import Cocoa import XCPlayground protocol ProtocolA : NSObjectProtocol { func functionA() } protocol ProtocolB : ProtocolA { func functionB() } extension ProtocolB { func functionA() { print("Passed functionA") } func functionB() { print("Passed functionB") } } class TestClass : NSObject, ProtocolB { override init () { } } var instance:TestClass = TestClass() instance.functionA() // Calls code OK.. if instance.respondsToSelector("functionA") { print("Responds to functionA") // **False, never passing here** } if instance.respondsToSelector("functionB") { print("Responds to functionB") // **False, never passing here** } 

Should be listed as an error?

+6
source share
1 answer

Interesting. Looks like me. It recognizes class functions, but not extensions. Regardless of instance type. Moreover, the extension code will not compile, since protocol methods are not optional. So does a bug / feature really look like? in response to the implementation of the selector.

+6
source

Source: https://habr.com/ru/post/989769/


All Articles