gpt4 book ai didi

用于检测可选关联类型的 Swift 泛型

转载 作者:行者123 更新时间:2023-11-28 15:33:11 25 4
gpt4 key购买 nike

<分区>

如何将扩展限制为仅在关联类型为可选时才匹配?

例如:

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?'相反。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com