2017-04-11 12 views
0

헤더에 다른 키 - 값 쌍이있는 키가있는 Alamofire 4 요청의 헤더를 설정하려고합니다. 신속한 컴파일러는 헤더 변수에 만족하지만 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 그 종류는 제 3 자 lib' SwiftyJson'. – BlueBoy

+0

예. SwiftyJSon을 사용하고 싶지 않으십니까? –

답변

0

당신이 Alamofire's source 살펴있는 경우 HTTPHeaders을 볼 수 실제로 [String: String]입니다 :

이 내가 가진 것입니다. 기본적으로 Alamofire[String: String]을 (를) headers으로 예상합니다.

subheaderString으로 변환하고 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 

당신은 그러나 당신이 원하는 오류 처리를 할 수있다, 나는 편의상 !의 사용.