简体   繁体   English

.request调用中的额外参数— Alamofire + Swift

[英]Extra argument in .request call — Alamofire + Swift

I'm trying to make a .request() call using Alamofire 4 and Swift 4. Here's the code I'm using: 我正在尝试使用Alamofire 4和Swift 4进行.request()调用。这是我正在使用的代码:

static func getPlaceData(url: String, parameters: [String: String]) {
    Alamofire.request(url, method: .get, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
        if response.result.isSuccess {
            let json: JSON = JSON(response.result.value!)
            print(json)
        } else {
            print("error")
        }
    }
}

where headers is: headers是:

let headers: HTTPHeaders = [
        "Authorization": "Bearer /*PRIVATE KEY*/"
]

I am getting the following error: Extra argument 'method' in call , at the Alamofire.request(... line. 我收到以下错误:在Alamofire.request(...行中, 在call中有额外的参数'method'

I've read through quite a few SO posts about similar issues but none of their solutions have fixed my problem: 我已经阅读了很多关于类似问题的SO帖子,但是他们的解决方案都不能解决我的问题:

I've also read in the this documentation guide and this issues thread on GitHub but neither have solved the issue. 我还阅读了本文档指南GitHub上的这个问题线程,但是都没有解决问题。

What is the proper syntax for a .request() call with required parameters and headers ? 具有所需parametersheaders.request()调用的正确语法是什么?

The code is for making an authenticated call from Swift code using the Yelp Fusion API -- maybe someone can suggest a better method than Alamofire? 该代码用于使用Yelp Fusion API从Swift代码进行身份验证调用-也许有人可以提出比Alamofire更好的方法?

I went through the documentations and found out two things 我浏览了文档,发现两件事

  1. The request method signature which is 请求方法签名是

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

Link to the documentation 链接到文档

  1. The Parameter typeAlias is a type of [String:Any] where as you are passing [String:String] which is conflicting. Parameter typeAlias是[String:Any]一种类型,在您传递[String:String]会发生冲突。

Link to the Parameter Definition in the DOC 链接到DOC中的参数定义

So all you need to do it change the signature and you are good. 因此,您要做的所有事情都可以更改签名,您就很好。

I figured out the issue, no thanks to any documentation or GitHub threads. 我解决了这个问题,不用多谢任何文档或GitHub线程。

The logic in the above code snippet (from the question) referenced a constant called headers , which was declared outside of getPlaceData(url:parameters:) . 上面的代码片段(来自问题)中的逻辑引用了一个名为headers的常量,该常量在getPlaceData(url:parameters:)外部声明。 This is because in order to access the Yelp Fusion API you need to include an extra field in the HTTP header you send to include the private API key using the format mentioned here . 这是因为要访问Yelp Fusion API,您需要在发送的HTTP标头中包含一个额外的字段,以使用此处提到的格式包含私有API密钥。

I moved the headers data structure inside of getPlaceData(url:parameters:) and it fixed the issue, so my resulting function is as follows: 我将headers数据结构移到了getPlaceData(url:parameters:) ,并解决了该问题,因此我得到的功能如下:

static func getPlaceData(url: String, parameters: [String: String]) {
  let headers: HTTPHeaders = [
    "Authorization": "Bearer /*PRIVATE KEY*/"
  ]

  Alamofire.request(url, method: .get, parameters: parameters, encoding: 
  JSONEncoding.default, headers: headers).responseJSON { (response) in
    if response.result.isSuccess {
      let json: JSON = JSON(response.result.value!)
      print(json)
    } else {
      print("error")
    }
  }
}

Not sure why this fixed the issue, but the code works now. 不确定为什么可以解决此问题,但是代码现在可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM