Alamofire 4错误请求“通话中有额外参数” [英] Alamofire 4 error request 'extra argument in call'
问题描述
我已经更新到Xcode8,swift3和Alamofire 4,现在在以'Alamofire.request'开头的下一行出现错误调用中的额外参数方法
I have updated to Xcode8, swift3 and Alamofire 4 and am now getting an error 'extra argument 'method' in call' on the below line beginning 'Alamofire.request'
var pulseVoteEndpoint: String = "https://example.com/app1/votingapi/set_votes.json"
var pulseNewVote = ["votes":[["value":valueString,"uid":uidString,"entity_id":nidInt,"entity_type":"node","tag":"points","value_type":"points"]]]
Alamofire.request(pulseVoteEndpoint, method: .post, parameters: pulseNewVote, encoding: .json)
.response { request, response, data, error in
debugPrint(response)
print(request)
print (response)
print (error)
}
有什么想法吗?
谢谢
推荐答案
request(...)
的 encoding
参数期望静态
属性,而不是枚举
例。
我们可以看一下静态 request(...)
方法的签名的 Alamofire
( Alamofire / Source / Alamofire.swift )
The encoding
argument of request(...)
expects static
properties, not enum
cases.
We can take a look at the signature of the static request(...)
method of Alamofire
(Alamofire/Source/Alamofire.swift)
public func request(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
-> DataRequest
{ /* ... */ }
第一个参数是符合协议 URLConvertible
的参数, 字符串
有效;到目前为止,一切都很好。
The first argument is one which conforms to the protocol URLConvertible
, to which String
is valid; all good so far.
第二个参数的类型为简单地将 .post
添加到枚举
是有效的情况,可以。
The type of the 2nd argument is simply an enum
to which .post
is a valid case, OK.
第三个参数的类型应为 Parameters
,即 [String:Any] $ c的类型别名$ c>
The third argument should be of type Parameters
, which is a typealias for [String: Any]
/// A dictionary of parameters to apply to a `URLRequest`.
public typealias Parameters = [String: Any]
您提供的参数 pulseNewVote
的类型为 Dictionary< String,Array< Dictionary< String,Any>>>
,但仍应能够被预期的参数类型( Dictionary< String,Any>
)使用。因此,即使您可能想考虑通过显式地将 pulseNewVote
的类型注释为 [String:Any]的类型来帮助Swift,
:
The argument you supply, pulseNewVote
, is of type Dictionary<String, Array<Dictionary<String, Any>>>
, but should still be able to be consumed by the expected argument type (Dictionary<String, Any>
). So this should still be OK, even if you might want to consider helping Swift out by explicitly type annotating the type of pulseNewVote
to [String: Any]
:
var pulseNewVote: [String: Any] = ...
第四个参数编码不是枚举
,但协议 ParameterEncoding
符合某些 struct
类型。这些类型具有静态成员(返回 self
的实例),在显式键入时可能看起来像 enum
例,但是必须包含显式键入
The fourth argument, the encoding, however, is not an enum
, but a protocol ParameterEncoding
to which a few struct
types conform. These type have static members (returning an instance of self
) which may look like enum
cases when explicitly type, but which must include the explicit typing
public struct JSONEncoding: ParameterEncoding {
// MARK: Properties
/// Returns a `JSONEncoding` instance with default writing options.
public static var `default`: JSONEncoding { return JSONEncoding() }
// ...
}
因此,您需要将提供给第四个参数 .json
的值替换为例如 JSONEncoding.default
。使用此修复程序,您对请求
的调用应如下所示:
Hence, you need to replace the value supplied to your fourth argument, .json
, with e.g. JSONEncoding.default
. With this fix, your call to request
should look like:
Alamofire.request(pulseVoteEndpoint, method: .post, parameters: pulseNewVote, encoding: JSONEncoding.default)
The错误(附加参数)来自响应
调用的完成处理程序。提供给响应
的调用的完成处理程序是需要单个 DefaultDataResponse
作为参数(尝试时不能使用4个不同的参数)。 DefaultDataResponse
类型的实例具有公共成员 request
, response
,数据
和错误
,您可以通过单个响应$ c进行访问完成处理程序中的$ c>参数(类型为
DefaultDataResponse
)。在您的响应
调用中调整完成处理程序以适应这一事实(使用来自Alamofire文档的示例,我们可以构建最终的请求
和响应
链式调用:
The error you're given (additional parameters) come from, however, the completion handler to the reponse
call. The completion handler supplied to a call to response
is one which takes a single DefaultDataResponse
as argument (not 4 different arguments as in your attempt). An instance of the DefaultDataResponse
type have public members request
, response
, data
and error
, which you may access via the single response
parameter (of type DefaultDataResponse
) in the completion handler. Adapting the completion handler in your response
call to this fact (using the example from the Alamofire documentation, we may construct the final request
and response
chained call:
Alamofire.request(pulseVoteEndpoint, method: .post, parameters: pulseNewVote, encoding: JSONEncoding.default)
.response { response in
// note that these are all optionals, so
// you might want to unwrap them first
print(response.request)
print(response.response)
print(response.error)
}
这篇关于Alamofire 4错误请求“通话中有额外参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!