2017-04-11 68 views
0

我試圖設置Alamofire 4請求的標頭,其中標頭的鍵值是另一個鍵值對。 swift編譯器很滿意我的頭文件變量,但Alamofire不會接受它們。在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 
} 
+0

檢查此http://stackoverflow.com/questions/41294012/alamofire-post-request-with-nested-json-parameters –

+0

@SivajeeBattina該sorta幫助,但不是真正的B/C他們正在使用第三方庫' SwiftyJson'。 – BlueBoy

+0

是的。你不想使用SwiftyJSon? –

回答

0

如果你看看Alamofire's source,你可以看到HTTPHeaders實際上是[String: String]。基本上,Alamofire預計[String: String]從您headers

您可以做的是將您的subheader轉換爲StringNSJSONSerialization並傳遞給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 

不管你想不想錯誤處理,爲了簡單起見,我使用了!