swift 带有额外反斜杠的错误消息解析时间
swift error message parsing time with extra backslashes
我正在开发一个 swiftui 项目,我需要 post 将特定日期格式的字符串调用到 api 并从中检索响应。
代码如下:
func createProfile(birthday: String, email: String, fullName: String, gender: String, completion: @escaping (Result<Bool, NetworkError>) -> Void) {
guard let url = URL(string: "*******") else {
completion(.failure(.invalidURL))
return
}
var request = URLRequest(url: url)
let defaults = UserDefaults.standard
guard let token = defaults.string(forKey: "jsonwebtoken") else {
return
}
request.httpMethod = "POST"
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body: [String: String] = [
"birthday" : "02/01/2022",
"email": email,
"fullName": fullName,
"gender": gender
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
print(body)
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
completion(.failure(.noData))
return
}
guard let profile = try? JSONDecoder().decode(ProfileResponse.self, from: data) else {
completion(.failure(.decodingError))
return
}
print(profile)
completion(.success(profile.activationStatus))
}.resume()
}
在没有日志记录的情况下,代码无法正常运行,应用程序正常运行,并显示下一个打印语句:
print(String(data: data, encoding: .utf8))
控制台记录:
Optional("{\"errors\":{\"message\":\"parsing time \\"\\\\"02\\\\/01\\\\/2022\\\\"\\" as \\"\\\\"02/01/2006\\\\"\\": cannot parse \\"\\\\/01\\\\/2022\\\\"\\" as \\"/\\"\"}}\n")
打印请求正文时也符合预期:
["birthday": "02/01/2022", "fullName": "Samy", "email": "hello.world@gmail.com", "gender": "Male"]
@Larme 的回答和link的建议帮助我修复了我的代码错误,感谢他:
无关但是:不需要 .fragmentsAllowed。问题是它会在字符串中的斜杠之前添加一个反斜杠。它是有效的 JSON,但它似乎与您的服务器解析有关。 print(body)
是误导,你想看到的是 JSON sent:
print(String(data: request.httpBody!, encoding: .utf8)!)
见 Codable 的 ,但似乎仅限于 iOS13+。但是您可能有一个 hint/lead 可以搜索...
我正在开发一个 swiftui 项目,我需要 post 将特定日期格式的字符串调用到 api 并从中检索响应。
代码如下:
func createProfile(birthday: String, email: String, fullName: String, gender: String, completion: @escaping (Result<Bool, NetworkError>) -> Void) {
guard let url = URL(string: "*******") else {
completion(.failure(.invalidURL))
return
}
var request = URLRequest(url: url)
let defaults = UserDefaults.standard
guard let token = defaults.string(forKey: "jsonwebtoken") else {
return
}
request.httpMethod = "POST"
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body: [String: String] = [
"birthday" : "02/01/2022",
"email": email,
"fullName": fullName,
"gender": gender
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
print(body)
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
completion(.failure(.noData))
return
}
guard let profile = try? JSONDecoder().decode(ProfileResponse.self, from: data) else {
completion(.failure(.decodingError))
return
}
print(profile)
completion(.success(profile.activationStatus))
}.resume()
}
在没有日志记录的情况下,代码无法正常运行,应用程序正常运行,并显示下一个打印语句:
print(String(data: data, encoding: .utf8))
控制台记录:
Optional("{\"errors\":{\"message\":\"parsing time \\"\\\\"02\\\\/01\\\\/2022\\\\"\\" as \\"\\\\"02/01/2006\\\\"\\": cannot parse \\"\\\\/01\\\\/2022\\\\"\\" as \\"/\\"\"}}\n")
打印请求正文时也符合预期:
["birthday": "02/01/2022", "fullName": "Samy", "email": "hello.world@gmail.com", "gender": "Male"]
@Larme 的回答和link的建议帮助我修复了我的代码错误,感谢他:
无关但是:不需要 .fragmentsAllowed。问题是它会在字符串中的斜杠之前添加一个反斜杠。它是有效的 JSON,但它似乎与您的服务器解析有关。 print(body)
是误导,你想看到的是 JSON sent:
print(String(data: request.httpBody!, encoding: .utf8)!)
见 Codable 的