【问题标题】:Alamofire 4 error request 'extra argument in call'Alamofire 4 错误请求“调用中的额外参数”
【发布时间】:2017-05-17 19:11:25
【问题描述】:

我已更新到 Xcode8、swift3 和 Alamofire 4,现在在以下行以 'Alamofire.request' 开头时收到错误'extra argument 'method' in call'

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

有什么想法吗?

谢谢

【问题讨论】:

  • 它说的是哪个额外的论点?
  • 哦,你可能需要指定pulseNewVote的类型...var pulseNewVote: [String: Any] = ...

标签: swift alamofire


【解决方案1】:

就我而言,问题出在 headers 参数中。它应该是 [字符串:字符串]

加上参数参数应该是[String:Any]

我使用 Alamofire 4.8.2 如下解决了这个编译错误:

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) 

【讨论】:

    【解决方案2】:

    我有一个 Xcode 错误导致发生此错误,因为部分 url 字符串在不需要时被强制解包。

    这是使用错误解包属性的调用:

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

    这是消除错误的固定调用:

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

    【讨论】:

    • 因为更容易在本地复制/粘贴并尝试复制问题(而不是将其全部输入到我的 xcode 中)
    • 好的,已更新代码以帮助其他人进行复制。谢谢
    【解决方案3】:

    request(...)encoding 参数需要 static 属性,而不是 enum 情况。

    我们可以看看Alamofire(Alamofire/Source/Alamofire.swift)的静态request(...)方法的签名

    public func request(
        _ url: URLConvertible,
        method: HTTPMethod = .get,
        parameters: Parameters? = nil,
        encoding: ParameterEncoding = URLEncoding.default,
        headers: HTTPHeaders? = nil)
        -> DataRequest
    { /* ... */ }
    

    第一个参数是符合URLConvertible协议的参数,String is valid;到目前为止一切顺利。

    第二个参数的类型是simply an enum.post 是一个有效的情况,OK。

    第三个参数的类型应该是Parameters,即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:

    var pulseNewVote: [String: Any] = ...
    

    然而,第四个参数,编码,不是enum,而是一个protocol ParameterEncoding,一些struct类型符合。这些类型具有静态成员(返回 self 的实例),在显式类型时可能看起来像 enum 情况,但必须包括显式类型

    public struct JSONEncoding: ParameterEncoding {
    
        // MARK: Properties
        /// Returns a `JSONEncoding` instance with default writing options.
        public static var `default`: JSONEncoding { return JSONEncoding() }
        // ...
    }
    

    因此,您需要将提供给第四个参数.json 的值替换为例如JSONEncoding.default。通过此修复,您对 request 的调用应如下所示:

    Alamofire.request(pulseVoteEndpoint, method: .post, parameters: pulseNewVote, encoding: JSONEncoding.default)
    

    但是,您收到的错误(附加参数)来自 reponse 调用的完成处理程序。提供给response 调用的完成处理程序是一个将单个DefaultDataResponse 作为参数的处理程序(而不是您尝试的4 个不同的参数)。 DefaultDataResponse 类型的实例具有公共成员 requestresponsedataerror,您可以通过完成处理程序中的单个 response 参数(DefaultDataResponse 类型)访问它们。在你的 response 调用中调整完成处理程序以适应这一事实(使用 example from the Alamofire documentation,我们可以构造最终的 requestresponse 链式调用:

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

    【讨论】:

    • PS 在我的代码中的另一种用法中,在“响应输入”行之后,我有“让值 = 响应.result.value else {”但这现在在 Xcode8 中不起作用。请问如何处理该响应?
    【解决方案4】:

    .json 代替编码,试试JSONEncoding.default

    您的参数应转换为:[String : Any]?

    这是编译器错误报告额外参数错误的问题。

    最后,你的行应该是这样的:

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

    【讨论】:

    • 非常感谢您的建议。我已经尝试过了,在“响应”上出现错误,上面写着“无法调用非函数类型 HTTPURLResponse 的值”。希望你能想到!
    • 对于这部分,请参阅@dfri 答案,因此我们不会在此处重复 - 它现在只有一个变量:包含请求、响应和错误的响应
    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多