- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在 Playground xcode 9.2 上运行时,苹果文档代码中出现上述错误 https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html
部分:使用类型注释来约束关联类型
protocol Container {
associatedtype Item: Equatable
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
protocol SuffixableContainer: Container {
associatedtype Suffix: SuffixableContainer where Suffix.Item == Item
func suffix(_ size: Int) -> Suffix
}
他们的解释:在这个协议(protocol)中,Suffix是一个关联类型,就像上面Container例子中的Item类型。 Suffix 有两个约束:它必须符合 SuffixableContainer 协议(protocol)(当前正在定义的协议(protocol)),它的 Item 类型必须与容器的 Item 类型相同。
最佳答案
protocol SuffixableContainer: Container {
associatedtype Suffix: SuffixableContainer
// ...
}
对关联类型使用“递归约束”:关联类型 Suffix
受限于封闭协议(protocol) SuffixableContainer
。
关联类型的递归约束已在 Swift 4.1 中实现,即该代码需要 Xcode 9.3(目前处于测试阶段)。
来自Swift CHANGELOG :
Swift 4.1
...
SE-0157 is implemented. Associated types can now declare "recursive" constraints, which require that the associated type conform to the enclosing protocol.
关于ios - 错误 : type may not reference itself as a requirement associatedtype Suffix: SuffixableContainer where Suffix. 项目 == 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49490509/
我一直在努力寻找在另一个关联类型中使用泛型和关联类型来解决上述问题的解决方案 案例 我想要一个ObjectRequestType,它里面有一个关联类型ObjectResponseType。 proto
这个问题在这里已经有了答案: Unable to use protocol as associatedtype in another protocol in Swift (2 个答案) Protoc
我一直在为这个问题苦苦思索 我正在尝试在 Swift 上创建一个绑定(bind)结构,以一种简单的方式绑定(bind) viewModel 和 Controller 。所以我创建了一个协议(proto
我正在尝试为我的 tableviewcells 设计一个面向协议(protocol)的 MVVM。我有很多。 我的 viewModel protocol PlainTableViewCellModel
这个问题在这里已经有了答案: Unable to use protocol as associatedtype in another protocol in Swift (2 个答案) 关闭 4 年
假设我有一个协议(protocol): protocol Router { associatedtype Answer typealias AnswerCallback = (Answ
在 swift 协议(protocol)中使用 generic with function 或使用 associatedType 有什么区别? protocol Repository { as
我来自 Java,我在学习模式方面没有什么困难。 我有第一个协议(protocol) protocol Interval { } 第二个: protocol Event { associate
我在使用关联类型作为协议(protocol)时遇到问题: protocol Searchable{ func matches(text: String) -> Bool } protoc
我有一个非常具体的问题,我无法通过 SO 上的给定答案解决这个问题。为了简化这个问题,我删除了所有不需要的东西。此代码仅显示理解问题所需的一切: 所以我有一个看起来像这样的类: class DataS
在设计 API 时,我在每个角落都会遇到这个问题: Protocol can only be used as a generic constraint because it has Self or a
所以这里我有这个协议(protocol) public protocol UseCase { associatedtype ResponseType associatedtype Pa
我正在使用一个库,它定义了两个协议(protocol),A 和 B,每个协议(protocol)都有它的 associatedtype T,像这样: protocol A { associat
这个有效: protocol Inconceivable { associatedtype AbstractType func say(it: AbstractType) } clas
我有一个协议(protocol) RequestType,它有如下所示的 associatedType 模型。 public protocol RequestType: class { ass
我正在尝试使用 MVVM 向 iOS 应用程序添加一些基类,以使其更容易并强制执行公共(public)类关系。 遗憾的是,我遇到了以下问题: error: MyPlayground.playgroun
这个问题在这里已经有了答案: Protocol doesn't conform to itself? (3 个答案) 关闭 3 年前。 我有以下代码: protocol User { var
我有一个小问题,我找不到一种优雅的方法来使用具有关联类型的闭包作为协议(protocol)中的 Void。 假设我有以下协议(protocol): protocol MyProtocol { as
当我在 Playground xcode 9.2 上运行时,苹果文档代码中出现上述错误 https://developer.apple.com/library/content/documentatio
我是一名优秀的程序员,十分优秀!