我有通用类型的协议。下面的代码正确吗 我收到错误消息:“无法推断出通用参数'T'”
我想念什么?
架构是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
})
}
}
答案 0 :(得分:0)
func showResults<Offer>(for service: APIService, _ completion: @escaping ([Offer]?, ErrorHandler?) -> Void) {
interactor?.getResults(for: service, { (results:ResultType, error:Error) in
})
}
您应该明确指定类型,我在操场上尝试过,这样就可以了。