ios - 静态成员不能用于协议(protocol)元类型

标签 ios swift proxy protocols associated-types

我想要完成的是制作代理协议(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 的类型。有什么解决方法吗?

最佳答案

好吧,你错过了一个步骤,例如:

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/

相关文章:

ios - 所有 View 顶部的公共(public)栏

ios - MKMapView 避免空白图 block [iOS 8+]

ios - Docker设置-无法运行世界

ios - 谷歌登录按钮按下检测 ios

arrays - 如何将动态数据快速获取到数组 KingFisher

android - Charles 代理和丢失的命中

c# - 如何在 .Net 应用程序中使用 IE 代理服务器设置及其凭据

html - 未安装应用程序时,我的应用程序不显示智能应用程序横幅

ios - 当前目标在 web View 中的 Facebook 授权期间 Conceal facebook ok 按钮

ios - 使用 NSFetchedResultsController 核心数据重新排序 TableView 单元格 - Swift 3