我想要完成的是制作代理协议,将我的课程路由到适当的服务。 我每1个代理服务有3种类型:OnlineService,OfflineService,DemoService,每种模式都有一种模式(在线,离线,演示)。
我创建了协议:
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
}
}
}
然后在客户代理类
上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)
}
}
但我收到了错误:
静态成员'customerDetails'不能用于协议metataype'CustomerServiceProxy.ServiceProtocol.Protocol'(又名'CustomerService.Protocol')。
我建议发生这种情况是因为代理服务变量返回CustomerService.Type而不是符合CustomerService的Type。这有什么解决方法吗?
答案 0 :(得分:0)
protocol Proxcy {}
extention Proxcy {
static func a() { print("A")
}
struct A: Proxcy {}
struct B: Proxcy {
A.a()
}