ios - 静态成员不能用于协议(protocol)元类型
<p><p>我想要完成的是制作代理协议(protocol),将我的类(class)路由到适当的服务。
每个代理有 3 种类型的服务:OnlineService、OfflineService、DemoService,每一种都适用于一种模式(在线、离线、演示)。</p>
<p>我创建了协议(protocol):</p>
<pre><code>protocol Proxy {
associatedtype ServiceProtocol
associatedtype OfflineServiceType: OfflineService
associatedtype OnlineServiceType: WebService
associatedtype DemoServiceType: DemoService
}
extension Proxy {
static var service: ServiceProtocol.Type {
if isOnlineMode() {
return OfflineServiceType.self as! ServiceProtocol.Type
} else if isDemoMode(){
return DemoServiceType.self as! ServiceProtocol.Type
}else{
return OnlineServiceType.self as! ServiceProtocol.Type
}
}
}
</code></pre>
<p>然后是客户代理类</p>
<pre><code>class CustomersServiceProxy: Proxy, CustomersService {
typealias ServiceProtocol = CustomersService
typealias OfflineServiceType = CustomersOfflineService
typealias OnlineServiceType = CustomerWebService
public static func customerDetails(for customer: Customer, completion: @escaping (CustomerDetails) -> Void) {
service.customerDetails(for: customer, completion: completion)
}
}
</code></pre>
<p>但我得到了错误:</p>
<blockquote>
<p>Static member 'customerDetails' cannot be used on protocol metataype 'CustomerServiceProxy.ServiceProtocol.Protocol' (aka 'CustomerService.Protocol').</p>
</blockquote>
<p>我建议发生这种情况,因为代理服务变量返回的是 CustomerService.Type,而不是符合 CustomerService 的 Type。有什么解决方法吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好吧,你错过了一个步骤,例如:</p>
<pre><code>protocol Proxcy {}
extention Proxcy {
static func a() { print("A")
}
struct A: Proxcy {}
struct B: Proxcy {
A.a()
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 静态成员不能用于协议(protocol)元类型,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46443366/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46443366/
</a>
</p>
页:
[1]