我在 Swift 中使用 ReactiveCocoa。我需要使用以下方法:
rac_signalForSelector(selector: Selector, fromProtocol: Protocol?)
通过 Selector("method:")
可以很好地传递选择器,但我找不到如何将委托(delegate)协议(protocol)传递给 fromProtocol
参数。
像这样将协议(protocol)类型从委托(delegate)传递到方法签名的正确方法是什么?
编辑:添加方法文档和最佳尝试
该方法的文档如下:
selector - The selector for whose invocations are to be observed. If it doesn’t exist, it will be implemented using information from protocol, and may accept non-object arguments and return a value. This cannot have C arrays or unions as arguments or return type.
protocol - The protocol in which selector is declared. This will be used for type information if the selector is not already implemented on the receiver. This must not be NULL, and selector must exist in this protocol.
我已尝试发送 DelegateType.self
,但最终收到此错误:
Could not find an overload for
rac_signalForSelector
that accepts the supplied arguments
最佳答案
你在声明你的协议(protocol)时是否使用了@objc
?
我相信 SomeProtocol.self
是传递它的正确方式,但由于您将它传递到 obj-c API,因此需要以 @objc
为前缀喜欢this example from the docs :
@objc protocol HasArea {
var area: Double { get }
}
编辑:原来有问题的协议(protocol)来自库(用 objective-c 编写,因此已经与 objective-c 兼容),而不是在 Swift 中定义。
在这种情况下,它可能是一个编译器错误,因此在您执行任何其他操作之前确保您使用的是最新版本的 Xcode(撰写本文时为 Beta 3)。
如果这没有解决,我认为 Tommy 使用 NSProtocolFromString
的想法是在修复编译器错误之前的最佳方法。
关于ios - @protocol(DelegateType) 的 Swift 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24616569/