我要完成的工作是制作将我的课程路由到适当服务的代理协议。我每1个代理人有3种服务:OnlineService,OfflineService,EnvoreService,每种模式之一(在线,离线,演示)。
我创建了协议:
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.serviceprotococol.protocol.protocol'(又名'customerservice.protocol')上使用。
我建议这样做是因为代理服务变量返回customerservice.type而不是符合客户服务的类型。有什么解决方法吗?
好吧,你错过了一个步骤,例如:
protocol Proxcy {}
extention Proxcy {
static func a() { print("A")
}
struct A: Proxcy {}
struct B: Proxcy {
A.a()
}