菜鸟教程小白 发表于 2022-12-11 19:44:44

ios - Swift:在协议(protocol)一致性中覆盖来自协议(protocol)扩展的关联类型


                                            <p><p>所以,我正在努力实现这一目标:</p>

<p>有一个带有关联类型的协议(protocol),它将处理 json 解析到他的扩展中。关联类型必须符合<code>Decodable</code>:</p>

<pre><code>protocol MyProtocol {
associatedtype ResponseType: Decodable
func handleResponse(data: Data) -&gt; ResponseType
}
</code></pre>

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

<pre><code>extension MyProtocol {
typealias ResponseType = MyDefaultDecodableType

func handleResponse(data: Data) -&gt; ResponseType { ... }
}

class MyObject: MyProtocol {
typealias ResponseType = AnotherDecodableType
}
</code></pre>

<p>问题是我在 <code>MyObject</code> 中遇到了这样的错误:</p>

<pre><code>error: type &#39;MyObject&#39; does not conform to protocol &#39;MyProtocol&#39;
class MyObject: MyProtocol {
      ^
note: multiple matching types named &#39;ResponseType&#39;
    associatedtype ResponseType: Decodable
                   ^
note: possibly intended match
typealias ResponseType = AnotherDecodableType
            ^
note: possibly intended match
    public typealias ResponseType = MyDefaultDecodableType
</code></pre>

<p>我不知道是否有可能实现我正在尝试的目标,或者我走错了路。谁能给我点灯?</p>

<p>谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我创建了相同的代码。这里有一些事实需要了解。</p>
<pre><code>extension MyProtocol {
    typealias ResponseType = MyDefaultDecodableType
   
    func handleResponse(data: Data) -&gt; ResponseType {
      
      return try! JSONDecoder().decode(MyDefaultDecodableType.self, from: data)
      
    }
}
</code></pre>
<p>从概念上讲,Swift 中没有通用协议(protocol)。但是通过使用 <code>typealias</code> 我们可以为另一种类型声明一个必需的别名。</p>
<p>您的扩展不需要定义 <code>typealias ResponseType = MyDefaultDecodableType</code>,因为它会使用 <code>MyDefaultDecodableType</code> 提供一些默认实现,所以它没用。</p>
<p>所以你的扩展会是这样的</p>
<pre><code>extension MyProtocol {
//typealias ResponseType = MyDefaultDecodableType // NO NEED FOR IT
   
    func handleResponse(data: Data) -&gt; MyDefaultDecodableType {
      print(&#34;Test \(self)&#34;)
      return try! JSONDecoder().decode(MyDefaultDecodableType.self, from: data)
      
    }
}
</code></pre>
<p>现在你可以定义</p>
<pre><code>class MyObject:MyProtocol {
    typealias ResponseType = AnotherDecodableType
   
    func handleResponse(data: Data) -&gt; ResponseType {
      print(&#34;Test \(self)&#34;)

      return try! JSONDecoder().decode(AnotherDecodableType.self, from: data)
      
    }
   
}
class MyObject2:MyProtocol {
   
   
}
</code></pre>
<p>没有任何错误</p>
<p>现在如果你使用</p>
<pre><code>MyObject().handleResponse(data:data)
MyObject2().handleResponse(data:data2)
</code></pre>
<p>你会得到</p>
<blockquote>
<p>test __lldb_expr_44.MyObject</p>
<p>test __lldb_expr_44.MyObject2</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift:在协议(protocol)一致性中覆盖来自协议(protocol)扩展的关联类型,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48791429/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48791429/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift:在协议(protocol)一致性中覆盖来自协议(protocol)扩展的关联类型