- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我一直在努力寻找在另一个关联类型中使用泛型和关联类型来解决上述问题的解决方案
我想要一个ObjectRequestType
,它里面有一个关联类型ObjectResponseType
。
protocol ObjectRequestType {
associatedtype Response: ObjectResponseType
}
另一方面,
ObjectResponseType
是一个具有关联类型 Element
protocol ObjectResponseType {
associatedtype Element
}
我想要实现的是,我想根据不同类型的元素扩展 ObjectRequestType
的功能,这里为了示例我们有两种不同类型的 Element
。
protocol ElementType {}
protocol OtherElementType {}
到目前为止,我将通过以下扩展来实现这一点
extension ObjectRequestType where Response.Element: ElementType {
static func request() {
print("ElementType")
}
}
extension ObjectRequestType where Response.Element: OtherElementType {
static func request() {
print("OtherElementType")
}
}
额外的步骤是处理将此请求传递给我需要使用泛型的类
class DemoClass<Request: ObjectRequestType> {
static func execute() {
Request.request()
}
}
由于 DemoClass
无法定义 Request
有什么样的 Response
,所以它会找到它的两个实现,并且会失败抛出编译错误
error: ambiguous reference to member 'request()'
通过添加额外的 where 子句来修改类仍然行不通,因为我会错过 OtherElementType
的其余实现
class DemoClass<Request: ObjectRequestType> where Request.Response.Element: ElementType {
static func execute() {
Request.request()
}
}
我一直在尝试解决这个问题,但我仍然无法实现这种情况。如果有人有任何想法或其他方法,我们将非常欢迎。
最佳答案
通常的做法是将 request
方法添加到 ObjectResponseType
协议(protocol)中,这样您就可以保证它存在于任何符合的类型上。然后你创建一个协议(protocol)扩展,为你知道如何处理的类型提供默认实现,你已经完成了。如果您需要使用现有元素类型之一覆盖某个请求的请求,您可以这样做。如果您需要支持其他元素类型,您可以直接在请求中执行或添加其他协议(protocol)扩展。
protocol ObjectResponseType {
associatedtype Element
}
protocol ObjectRequestType {
associatedtype Response: ObjectResponseType
static func request()
}
protocol ElementType {}
extension ObjectRequestType where Response.Element: ElementType {
static func request() {
print("ElementType")
}
}
protocol OtherElementType {}
extension ObjectRequestType where Response.Element: OtherElementType {
static func request() {
print("OtherElementType")
}
}
class DemoClass<Request: ObjectRequestType> {
static func execute() {
Request.request()
}
}
class Foo: ElementType {}
class FooResponse: ObjectResponseType {
typealias Element = Foo
}
class FooRequest: ObjectRequestType {
typealias Response = FooResponse
}
class Bar: OtherElementType {}
class BarResponse: ObjectResponseType {
typealias Element = Bar
}
class BarRequest: ObjectRequestType {
typealias Response = BarResponse
// Override the default implementation
static func request() {
print("Bar")
}
}
class Baz {}
class BazResponse: ObjectResponseType {
typealias Element = Baz
}
class BazRequest: ObjectRequestType {
typealias Response = BazResponse
static func request() {
print("Baz")
}
}
DemoClass<FooRequest>.execute() // ElementType
DemoClass<BarRequest>.execute() // Bar
DemoClass<BazRequest>.execute() // Baz
关于swift - associatedType 在另一个 associatedType 中使用协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43925216/
我一直在努力寻找在另一个关联类型中使用泛型和关联类型来解决上述问题的解决方案 案例 我想要一个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
我是一名优秀的程序员,十分优秀!