无法弄清楚如何使用 Alamofire .post方法将日期传递到服务器。我必须像这样形成JSON主体:
{
"title": "My Title",
"locations": [
{
"location": "locationID"
}
],
}
我坚持"位置" 属性。可能它必须是一个具有一个location属性的对象Array,这是一个字符串类型。目前我的代码是:
@IBAction func createEvent(_ sender: Any) {
let parameters: Parameters = [
"title": Event.title ?? nil,
"locations": //What have I wright here?
]
Alamofire.request(requestURL,
method: .post,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers).responseJSON {response in
switch response.result {
case .success:
print(response)
}
case .failure(let error):
print(error)
}
}
}
请帮忙。
答案 0 :(得分:2)
你可以尝试
let parameters: Parameters = [
"title": Event.title ?? nil,
"locations": [["location":"idherrr"]]
]
答案 1 :(得分:1)
如果您的API请求接受特定字符串格式,那么您需要使用DateFormatter转换字符串格式中的日期,否则如果接受日期对象,则传递但是Date对象,但这可能是不可能的。所以请尝试使用第一个选项转换日期为字符串格式,这是服务器参数预定的。
或者如果可能,请在您的服务器上共享请求参数类型支持。
答案 2 :(得分:1)
你可以试试
let parameters: Parameters = [
"title": Event.title ?? nil,
"locations": [dictionary]]
]