【发布时间】:2016-11-02 19:12:54
【问题描述】:
在下面的代码中,我想测试x 是否为SpecialController。如果是,我想将currentValue 作为SpecialValue。你怎么做到这一点?如果不使用演员表,则可以使用其他技术。
最后一行不会编译。错误是:Protocol "SpecialController" 只能用作泛型约束,因为它有 Self 或关联的类型要求。
protocol SpecialController {
associatedtype SpecialValueType : SpecialValue
var currentValue: SpecialValueType? { get }
}
...
var x: AnyObject = ...
if let sc = x as? SpecialController { // does not compile
【问题讨论】:
标签: swift generics swift3 protocols associated-types