【发布时间】:2019-04-25 02:08:00
【问题描述】:
我必须接收 JSON 数据,指定我需要传递给 url 的有效负载。它看起来像这样:
{
"token": "string",
"body": {
"token": "another string",
"intValue": 1,
"somethingElse": "another string"
}
}
我知道 body 将具有始终为字符串或数字的属性。但我不知道与字符串或整数值配对的关键是什么。
然后我需要用 Alamofire 发送那个身体,我这样做是这样的:
// parsing the data received with the Codable:
struct NotificationDetails: Codable {
let token: String
let body: [String:String]?
let
init (token: String) {
self.token = token
self.body = nil
}
init (token: String, body: [String:String]) {
self.token = token
self.body = body
}
}
请注意,我不能只使用 Alamofire 方便的参数类型,例如:
struct NotificationDetails: Codable {
let token: String
let body: Parameters?
init (token: String) {
self.token = token
self.body = nil
}
init (token: String, body: Parameters) {
self.token = token
self.body = body
}
}
因为它是不可编码的,就像 AnyObject 一样。
// then elsewhere, using that data
guard let bodyJSON = localNotificationDetails.body else {
callback()
return
}
AF.request(url, method:method, parameters: bodyJSON).responseJSON(queue: queue) { response in
但是现在我看到我的一些数据将是数字而不是字符串,我想知道我现在该怎么做?
【问题讨论】: