Jan*_*bny 6 cocoa swift swift2
我不确定,在我看来,它是Swift 2.0中的协议扩展的某种错误或不良实现.
我有protocolA,protocolB扩展protocolA和protocolB扩展中的实现方法.
我已经使类实例符合protocolB,但是当respondsToSelector检查protocolA/B方法时,结果是错误的.
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**
}
Run Code Online (Sandbox Code Playgroud)
应该报告为bug?
有趣.对我来说看起来像个错误.它确实识别类的功能,但不识别扩展功能.无论Instance有什么类型.此外,没有扩展代码将无法编译,因为协议方法是非可选的.那真的看起来像一个bug /功能?响应选择器实现.
归档时间: |
|
查看次数: |
792 次 |
最近记录: |