<分区>
如何将扩展限制为仅在关联类型为可选时才匹配?
例如:
protocol FooProtocol: class {
associatedtype BarType
var contents: BarType { get }
}
extension FooProtocol where BarType: Optional<Any> {
func unwrap() -> BarType {
return self.contents!
}
}
class BazClass: FooProtocol {
typealias BarType = String?
var contents: BarType
}
此错误与 Type 'Self.BarType' constrained to non-protocol type 'Optional<Any>'
在扩展的左括号上。我也试过 Any?
, BarType?
代替 Optional<Any>
.最后一个错误 Inheritance from non-named type 'BarType?'
相反。