Swift 2,协议扩展& responsesToSelector [英] Swift 2, protocol extensions & respondsToSelector

查看:152
本文介绍了Swift 2,协议扩展& responsesToSelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定,它看起来对我来说,这是一种错误或错误的实施与协议扩展在Swift 2.0。

I am not sure, it looks to me that it is some kind of bug or bad implementation with protocol extensions in Swift 2.0.

我有protocolA,protocolB扩展protocolA并在protocolB扩展中实现方法。

I have protocolA, protocolB extending protocolA and implementing methods in protocolB extension.

我已经符合类实例以符合protocolB,但是当通过respondToSelector检查protocolA / B方法时,结果为假。

I have conformed an class instance to conform to protocolB, however when inspected by respondsToSelector for protocolA/B methods the results is 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**
}

应该报告为错误?

推荐答案

有趣。看起来像一个bug给我。它确实识别类上的函数,但不识别扩展。无论实例具有什么类型。此外,没有扩展代码将不可编译,因为协议方法是非可选的。所以真的看起来像一个错误/功能?以响应选择器实现。

Interesting. Looks like a bug to me. It does recognize functions on a class, but not on extension. No matter what type Instance has. Moreover without an extension code would not be compilable, since protocol methods are non optional. So really looks like a bug/feature? in responds to selector implementation.

这篇关于Swift 2,协议扩展& responsesToSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆