Swift类不符合从swift协议继承的目标c协议

时间:2017-09-28 01:24:26

标签: ios swift swift3

当从swift协议继承时,我遇到检查objective-c协议一致性的问题。 据我所知,以下代码应该打印为true。 (斯威夫特3)

import UIKit

protocol MyProtocol: UITableViewDelegate {}

class MyClass: UIViewController, MyProtocol {}

let myClass = MyClass()

print(myClass.conforms(to: UITableViewDelegate.self))
// prints false 

let viewController = myClass as UIViewController
print(viewController as? UITableViewDelegate ?? "not a delegate")
// prints not a delegate

如果有人知道为什么会这样,或者如何正确检查这种一致性是否很好

2 个答案:

答案 0 :(得分:2)

在Swift 3中,将@objc添加到我的MyProtocol即可。

答案 1 :(得分:1)

为什么它对我来说是 true (使用Swift 3.2和4.0测试过),我们是否也做了同样的事情:

enter image description here

相关问题