在Alamofire中设置具有嵌套键值的标头

时间:2017-04-11 03:44:17

标签: swift alamofire alamofireimage

我正在尝试设置Alamofire 4请求的标头,其中标头有一个键,其值是另一个键值对。 swift编译器对我的标题变量感到满意,但Alamofire不会接受它们。

这就是我所拥有的:

 let subHeader = [
  "some-key": "some-value",
  "another-oey": "another-value"
  ]

let headers : [String: Any] = [
  "some-header-key": "some-header-value",
  "subdata": [subHeader]
  ]

// Upload data                  
Alamofire.upload(data,
   to: uploadurl,
   method: .put,
   headers: headers)
   .validate()
   .response { response in
   // Handle response
 }

1 个答案:

答案 0 :(得分:0)

如果查看Alamofire's source,您可以看到HTTPHeaders实际上是[String: String]。基本上,Alamofire期望您[String: String] headers

您可以做的是将subheader转换为String NSJSONSerialization并将其传递给Alamofire

let dictData = try! JSONSerialization.data(withJSONObject: subheader)
let dictString = String(data: dictData, encoding: String.Encoding.utf8)!
print(dictString)
// Now use "dictString" instead of "subheader" in your "headers" dictionary

您可以根据需要进行错误处理,为简单起见,我使用了!