Alamofire 4 error request 'extra argument in call'

后端 未结 4 1874
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 03:43

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\'

4条回答
  •  长情又很酷
    2021-01-28 04:04

    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>>, but should still be able to be consumed by the expected argument type (Dictionary). 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)
    }
    

提交回复
热议问题