通过继续这个问题,
我正在尝试转换[String : Any]
为String
然后将其传递String
给forHTTPHeaderField
尝试1:没有漂亮
let encoder = JSONEncoder()
if let json = try? encoder.encode(jsonDict) {
convertedString = String(data: json, encoding: .utf8)!
}
print("JsonStringFormat ", convertedString )
let url = NSURL(string: getMenuURL)
let request = NSMutableURLRequest(url: url! as URL)
request.setValue(convertedString, forHTTPHeaderField: "SessionInfo")
print("\nHEADer__reQQ__ ", request.allHTTPHeaderFields)
输出:
JsonStringFormat {"Token":"96FFC5B994514B3D","UICulture":"en-CA ","LanguageCode":"ENG","CompanyID":"QAP","IMEINo":"1jzBG3TSrMzj\/tKihlEv8g=="}
HEADer__reQQ__ ["SessionInfo": "{\"Token\":\"96FFC5B994514B3D\",\"LanguageCode\":\"ENG\",\"UICulture\":\"en-CA \",\"CompanyID\":\"QAP\",\"IMEINo\":\"1jzBG3TSrMzj\\/tKihlEv8g==\"}"]
尝试 2:打印 .pretty
let encoder = JSONEncoder()
// ADDING PRETTY FORMAT
encoder.outputFormatting = .prettyPrinted
if let json = try? encoder.encode(jsonDict) {
convertedString = String(data: json, encoding: .utf8)!
}
print("PrettyJsonStringFormat ", convertedString )
let url = NSURL(string: getMenuURL)
let request = NSMutableURLRequest(url: url! as URL)
request.setValue(convertedString, forHTTPHeaderField: "SessionInfo")
print("\nPrettyHeader__ ", request.allHTTPHeaderFields)
输出:
PrettyJsonStringFormat {
"Token" : "70E277954143414A",
"UICulture" : "en-CA ",
"LanguageCode" : "ENG",
"CompanyID" : "QAP",
"IMEINo" : "1jzBG3TSrMzj\/tKihlEv8g=="
}
PrettyHeader__ [:]
如果我选择尝试 1,BackSlash \
则附加该值。为了避免这种情况,我选择了尝试 2 [Pretty Printed]
,.
我不知道为什么request.allHTTPHeaderFields
没有添加标题值。
请指导我。