Ios 静态成员不能用于协议元类型
我试图完成的是制定代理协议,将我的类路由到适当的服务。 我有3种类型的服务,每1个代理:在线服务,离线服务,演示服务,在线,离线,演示模式 我创建了协议:Ios 静态成员不能用于协议元类型,ios,swift,proxy,protocols,associated-types,Ios,Swift,Proxy,Protocols,Associated Types,我试图完成的是制定代理协议,将我的类路由到适当的服务。 我有3种类型的服务,每1个代理:在线服务,离线服务,演示服务,在线,离线,演示模式 我创建了协议: protocol Proxy { associatedtype ServiceProtocol associatedtype OfflineServiceType: OfflineService associatedtype OnlineServiceType: WebService associatedtyp
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”不能用于协议元类型“CustomerServiceProxy.ServiceProtocol.protocol”aka“CustomerService.protocol”
我认为这是因为代理服务变量返回的是CustomerService.Type,而不是符合CustomerService的类型。有什么解决办法吗?嗯,你缺少一个步骤,例如:
protocol Proxcy {}
extention Proxcy {
static func a() { print("A")
}
struct A: Proxcy {}
struct B: Proxcy {
A.a()
}
嗯,你缺少一个步骤,例如:
protocol Proxcy {}
extention Proxcy {
static func a() { print("A")
}
struct A: Proxcy {}
struct B: Proxcy {
A.a()
}
大家好,欢迎来到SO!虽然这段代码可以回答这个问题,但提供关于它如何和/或为什么解决问题的附加上下文将提高答案的长期价值。请阅读,大家好,欢迎来到SO!虽然这段代码可以回答这个问题,但提供关于它如何和/或为什么解决问题的附加上下文将提高答案的长期价值。请阅读,然后