【问题标题】:Swift: override associatedtype from protocol extension in protocol conformanceSwift:在协议一致性中覆盖来自协议扩展的关联类型
【发布时间】:2018-07-25 06:38:16
【问题描述】:

所以,我正在努力实现这一目标:

有一个带有关联类型的协议,它将处理 json 解析到他的扩展中。关联类型必须符合Decodable:

protocol MyProtocol {
  associatedtype ResponseType: Decodable
  func handleResponse(data: Data) -> ResponseType
}

我想要做的是将 responseType 的默认类型设置到我的扩展中,然后,如果需要,将该类型覆盖到类或结构一致性中。像这样。

extension MyProtocol {
  typealias ResponseType = MyDefaultDecodableType

  func handleResponse(data: Data) -> ResponseType { ... }
}

class MyObject: MyProtocol {
  typealias ResponseType = AnotherDecodableType
}

问题是我在MyObject 中遇到了这样的错误:

error: type 'MyObject' does not conform to protocol 'MyProtocol'
class MyObject: MyProtocol {
      ^
note: multiple matching types named 'ResponseType'
    associatedtype ResponseType: Decodable
                   ^
note: possibly intended match
  typealias ResponseType = AnotherDecodableType
            ^
note: possibly intended match
    public typealias ResponseType = MyDefaultDecodableType

我不知道是否有可能实现我正在尝试的目标,或者我正在接近错误的方式。谁能给我点灯?

谢谢。

【问题讨论】:

  • 您的 MyDefaultDecodableType 和 AnotherDecodableType 看起来如何?
  • AnotherDecodableType 和 MyDefaultDecodableType 正在确认Decodable 协议?
  • 感谢您的回答,我很高兴它对您有所帮助:) 祝您有美好的一天

标签: ios swift protocols


【解决方案1】:

我创建了相同的代码。这里有一些事实需要了解。

extension MyProtocol {
    typealias ResponseType = MyDefaultDecodableType
    
    func handleResponse(data: Data) -> ResponseType {
        
        return try! JSONDecoder().decode(MyDefaultDecodableType.self, from: data)
        
    }
}

从概念上讲,Swift 中没有通用协议。但是通过使用typealias,我们可以为另一种类型声明一个必需的别名。

你的扩展不需要定义typealias ResponseType = MyDefaultDecodableType,因为它会使用MyDefaultDecodableType提供一些默认实现,所以它没用。

所以你的扩展会是这样的

extension MyProtocol {
  //  typealias ResponseType = MyDefaultDecodableType // NO NEED FOR IT
    
    func handleResponse(data: Data) -> MyDefaultDecodableType {
        print("Test \(self)")
        return try! JSONDecoder().decode(MyDefaultDecodableType.self, from: data)
        
    }
}

现在你可以定义

class MyObject:MyProtocol {
    typealias ResponseType = AnotherDecodableType
    
    func handleResponse(data: Data) -> ResponseType {
        print("Test \(self)")

        return try! JSONDecoder().decode(AnotherDecodableType.self, from: data)
        
    }
    
}
class MyObject2:MyProtocol {
   
    
}

没有任何错误

现在如果你使用

MyObject().handleResponse(data:data)
MyObject2().handleResponse(data:data2)

你会得到

测试 __lldb_expr_44.MyObject

测试 __lldb_expr_44.MyObject2

【讨论】:

    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多