High-speed equivalent for @protocol (DelegateType)

I am working with ReactiveCocoa in Swift. I need to use the following method:

rac_signalForSelector(selector: Selector, fromProtocol: Protocol?)

Passing a selector works fine with Selector("method:") , but I cannot find how to pass a delegate protocol to the fromProtocol parameter.

What is the correct way to pass a protocol type from a delegate to a method signature this way?

EDIT: Adding Method Documentation and Best Attempt

The documentation for this method is as follows:

selector - selector whose calls must be respected. If it does not exist, it will be implemented using information from the protocol and can take arguments without an object and return a value. It cannot have C-arrays or associations as arguments or return type.

protocol . The protocol in which the selector is declared. This will be used for type information if the selector is not yet implemented on the receiver. It must not be NULL, and a selector must exist in this protocol.

I tried sending to DelegateType.self , and I get this error:

Could not find an overload for rac_signalForSelector that takes the provided arguments

+6
source share
1 answer

Did you @objc when you announced your protocol?

I believe SomeProtocol.self is the right way to pass it, but since you are passing it to the obj-c API, it should have the @objc prefix, for example this example from the docs :

 @objc protocol HasArea { var area: Double { get } } 

Change It turns out that this protocol belongs to a library (written in objective-c, therefore already compatible with objective-c), not specified in Swift.

In this case, this is probably a compiler error, so before you do anything else , make sure you are using the latest version of Xcode (beta 3 at the time of writing).

If this does not work, I think Tommy's idea of ​​using NSProtocolFromString is the best problem until the compiler error is fixed.

+4
source

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


All Articles