首页 > 解决方案 > 在 Swift 4.1 中,具体类型不能符合具有约束关联类型的协议

问题描述

我有一个小问题,这是错误的代码:

protocol ConstraintProtocol { }

protocol AssocProtocol {
    associatedtype T : ConstraintProtocol
}

protocol MyProtocol : ConstraintProtocol { }

enum ConcreteType: AssocProtocol {
    typealias T = MyProtocol
    case a
}

该错误表明通常type 'ConcreteType' does not conform to protocol 'AssocProtocol'甚至给出提示:possibly intended match 'ConcreteType.T' (aka 'MyProtocol') does not conform to 'ConstraintProtocol'

但是……MyProtocol 确实符合ConstraintProtocol!!!

事实上,如果我从 中删除约束associatedtype,它就像一个魅力。

那么,只是我,还是我错过了什么?

如果有帮助的话,我在 Xcode 9.3.1 中使用 Swift 4.1。

标签: swiftgenericsprotocolsassociated-types

解决方案


推荐阅读