我想要完成的是制作代理协议(protocol),将我的类(class)路由到适当的服务。 每个代理有 3 种类型的服务:OnlineService、OfflineService、DemoService,每一种都适用于一种模式(在线、离线、演示)。
我创建了协议(protocol):
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)
}
}
但我得到了错误:
Static member 'customerDetails' cannot be used on protocol metataype 'CustomerServiceProxy.ServiceProtocol.Protocol' (aka 'CustomerService.Protocol').
我建议发生这种情况,因为代理服务变量返回的是 CustomerService.Type,而不是符合 CustomerService 的 Type。有什么解决方法吗?
好吧,你错过了一个步骤,例如:
protocol Proxcy {}
extention Proxcy {
static func a() { print("A")
}
struct A: Proxcy {}
struct B: Proxcy {
A.a()
}
关于ios - 静态成员不能用于协议(protocol)元类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46443366/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |