Alamofire 4 error request 'extra argument in call'

给你一囗甜甜゛ 提交于 2019-12-31 05:22:05

问题


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)
         }

Any ideas?

Thank you


回答1:


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
{ /* ... */ }

The first argument is one which conforms to the protocol URLConvertible, to which String is valid; all good so far.

The type of the 2nd argument is simply an enum to which .post is a valid case, OK.

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]

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] = ...

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() }
    // ...
}

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 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)
}



回答2:


Instead of .json for encoding, try JSONEncoding.default.

Your paramters should be casted to: [String : Any]?

This is an issue with compiler which falsely report error for extra argument.

In the end, your line should look like this:

Alamofire.request(pulseVoteEndpoint, method: .post, parameters: pulseNewVote as [String : Any]?, encoding: JSONEncoding.default)
         .response { request, response, data, error in
// rest of the code



回答3:


I had an Xcode bug causing this error to occur because part of the url string was being force unwrapped when it didn't need to be.

Here is the call with the incorrectly unwrapped property:

Alamofire.request(baseUrl + "notes/\(note.id!)", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { [weak self] response in

Here is the fixed call that will eliminate the error:

Alamofire.request(baseUrl + "notes/\(note.id)", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { [weak self] response in



回答4:


In my case The problem was in the headers argument. It should be [String:String]

Plus the parameters argument should be [String:Any]

I used Alamofire 4.8.2 as the below to resolve this compilation error:

let parameters = ["key":"value","key2":1] as [String:Any]
let headers = ["header_1":"value"] as [String:String]
let encoding:URLEncoding = URLEncoding.default

Alamofire.request("base_url",
                   method: .post,
                   parameters: parameters,
                   encoding: encoding,
                   headers: headers) 


来源:https://stackoverflow.com/questions/41447819/alamofire-4-error-request-extra-argument-in-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!