swift - Alamofire 4 错误请求 'extra argument in call'

标签 swift alamofire

我已经更新到 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)
         }

有什么想法吗?

谢谢

最佳答案

encoding request(...) 的参数期待 static属性,而不是 enum例。

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

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

第一个参数是一个符合协议(protocol) 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 的类型来帮助 Swift。至 [String: Any] :

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 的实例类型有公共(public)成员 request , response , dataerror ,您可以通过单个 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)
}

关于swift - Alamofire 4 错误请求 'extra argument in call',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41447819/

相关文章:

ios - Alamofire 错误 token (发布)与 Swift

arrays - Swift - append 到结构中的数组

ios - 在多个类中添加 touchesBegan

ios - 在 Alamofire 中使用参数上传图像时发送到实例的无法识别的选择器

arrays - 如何在本地数组 swift 3 中保存来自 Alamofire 的 response.result.value

ios - Alamofire 上传完成 block 在 iOS 9 和 IOS 11 上正常工作但在 iOS 10 上错误

ios - 在 Swift 中旋转文本

ios - PUT 方法的 Alamofire Content-Type application/json

swift - UIStatusBarStyle 没有改变

swift - 迁移到 swift 3 后,Alamofire 对成员 request() 问题的模糊引用