无法推断出通用参数“ T”-可编码

时间:2019-06-08 16:03:34

标签: swift generics codable

我有通用类型的协议。下面的代码正确吗 我收到错误消息:“无法推断出通用参数'T'”

generic type error

我想念什么?

架构是VIPER,所以我有一个View,Presenter,Interactor ...

Protocols.swift

protocol OffersViewProtocol {
    var presenter: OffersPresenterProtocol? { get set }
    func setup()
    func displayResults()
}

protocol OffersPresenterProtocol {
    var interactor: OffersInteractorProtocol? { get set }
    var router: OffersRouterProtocol? { get set }
    func showResults<T: Codable>(for service: APIService,_ completion:  @escaping ResultsHandler<T>)
}

protocol OffersInteractorProtocol {
   func getResults<T: Codable>(for service: APIService,_ completion: @escaping ResultsHandler<T>)
}

Interactor.swift

class OffersInteractor: OffersInteractorProtocol {
    let dataManager = DataManagerService()
    func getResults<Offer>(for service: APIService, _ completion: @escaping ([Offer]?, ErrorHandler?) -> Void) {
        dataManager.get(for: service) { results in

        }
    }

}

Presenter.swift

class OffersPresenter: OffersPresenterProtocol {


    var interactor: OffersInteractorProtocol?

    var router: OffersRouterProtocol?

    func showResults<Offer>(for service: APIService, _ completion: @escaping ([Offer]?, ErrorHandler?) -> Void) {
        interactor?.getResults(for: service, { (results, error) in

        })
    }
}

1 个答案:

答案 0 :(得分:0)

    func showResults<Offer>(for service: APIService, _ completion: @escaping ([Offer]?, ErrorHandler?) -> Void) {
        interactor?.getResults(for: service, { (results:ResultType, error:Error) in

        })
    }

您应该明确指定类型,我在操场上尝试过,这样就可以了。