我遇到了编译错误,我在解释时遇到了问题。预期的参数类型看起来与我传递给它的相同,所以我不确定该怎么做。可能最好只是粘贴错误和相关的代码位。
这里失败了:
static func DefaultProvider() -> ReactiveManabiAPIProvider<ManabiAPI> {
return ReactiveManabiAPIProvider(endpointClosure: endpointsClosure,
requestClosure: endpointResolver(),
stubClosure: StubBehaviour,
plugins: APIProvider.plugins)
}
错误信息:
错误:无法转换类型&#39;(ManabiAPI)的值 - &gt;端点&#39;预期参数类型&#39; _ - &gt;端点&LT; _&GT;&#39; 返回ReactiveManabiAPIProvider(endpointClosure:endpointsClosure, ^ ~~~~~~~~~~~~~~~
以上引用的其他一些相关代码:
enum ManabiAPI {
[...]
}
extension ManabiAPI : TargetType {
var baseURL: NSURL { return NSURL(string: "[...]")! }
var path: String {
[...]
}
var parameters: [String: AnyObject]? {
[...]
}
var method: ReactiveMoya.Method {
[...]
}
[...]
}
public class ReactiveManabiAPIProvider<Target where Target: TargetType> : ReactiveCocoaMoyaProvider<Target> {
var appController: AppController
var requestErrors: RACSubject
override init(endpointClosure: MoyaProvider<Target>.EndpointClosure = MoyaProvider.DefaultEndpointMapping,
requestClosure: MoyaProvider<Target>.RequestClosure = MoyaProvider.DefaultRequestMapping,
stubClosure: MoyaProvider<Target>.StubClosure = MoyaProvider.NeverStub,
manager: Manager = Alamofire.Manager.sharedInstance,
plugins: [PluginType] = [], stubScheduler: DateSchedulerType? = nil) {
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
appController = appDelegate.appController!
requestErrors = RACSubject()
super.init(endpointClosure: endpointClosure, requestClosure: requestClosure, stubClosure: stubClosure, manager: manager, plugins: plugins)
}
[...]
}
如果能看到其他任何内容,请告诉我。
答案 0 :(得分:1)
我很难在操场上尝试这个解决方案,因为上面的例子仍然有点太小。但我怀疑您需要在函数Target
的return子句中的TargetType
初始化程序中明确声明泛型ReactiveManabiAPIProvider
类型(在类型约束DefaultProvider()
下)。 E.g:
static func DefaultProvider() -> ReactiveManabiAPIProvider<ManabiAPI> {
return ReactiveManabiAPIProvider<ManabiAPI>(endpointClosure: endpointsClosure,
requestClosure: endpointResolver(),
stubClosure: StubBehaviour,
plugins: APIProvider.plugins)
}
否则返回初始化ReactiveManabiAPIProvider
而没有关联的泛型,并且自然后面的返回强制转换失败,因为函数返回期望ReactiveManabiAPIProvider
实例具有泛型Target
类型ManabiAPI
。
答案 1 :(得分:0)
这就是我遇到完全相同问题的代码(我使用的是Moya 6.5.0)
public protocol ApiService {
var apiProvider: ReactiveCocoaMoyaProvider<ApiRouter> { get }
func request(token: ApiRouter) -> SignalProducer<Response, NSError>
}
public class ApiServiceImpl: ApiService {
private let application: UIApplication
public var apiProvider: ReactiveCocoaMoyaProvider<ApiRouter>
public init(stubbing: Bool, application: UIApplication) {
self.application = application
if stubbing == false {
let endpointsClosure = { (target: ApiRouter) -> Endpoint<ApiRouter> in
return Endpoint<ApiRouter>(
URL: ApiServiceImpl.url(target),
sampleResponseClosure: { .NetworkResponse(200, target.sampleData) },
method: target.method,
parameters: target.parameters
)
}
let networkActivityPlugin = NetworkActivityPlugin { type in
switch type {
case .Began : application.networkActivityIndicatorVisible = true
case .Ended : application.networkActivityIndicatorVisible = false
}
}
let networkLoggerPlugin = NetworkLoggerPlugin(verbose: true, responseDataFormatter: JSONResponseDataFormatter)
let plugins = [networkActivityPlugin, networkLoggerPlugin]
apiProvider = ReactiveCocoaMoyaProvider<ApiRouter>(endpointClosure: endpointsClosure, plugins: plugins)
} else {
apiProvider = ReactiveCocoaMoyaProvider<ApiRouter>(stubClosure: { _ in .Immediate })
}
}
public func request(token: ApiRouter) -> SignalProducer<Response, NSError> {
return apiProvider
.request(token)
.filterSuccessfulStatusAndRedirectCodes()
}
private static func url(route: TargetType) -> String {
return route.baseURL.URLByAppendingPathComponent(route.path).absoluteString
}
}
我通过将 [PluginType] 添加到插件数组定义行来修复该错误,因此它变为
let plugins: [PluginType] = [networkActivityPlugin, networkLoggerPlugin]