【问题标题】:How to pass data in JSON Encoding with Alamofire Swift 4如何使用 Alamofire Swift 4 在 JSON 编码中传递数据
【发布时间】:2018-03-20 11:18:08
【问题描述】:

无法弄清楚如何使用 Alamofire .post 方法将日期传递给服务器。我必须像这样形成 JSON 主体:

{
    "title": "My Title",
    "locations": [
        {
            "location": "locationID"
        }
        ],
}

我被困在 "locations" 属性上。可能它必须是一个对象数组,其中一个位置属性是字符串类型。目前我的代码是:

@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)
                        }
    }
}

请帮忙。

【问题讨论】:

    标签: ios iphone swift alamofire


    【解决方案1】:

    你可以试试

    let parameters: Parameters = [
        "title": Event.title ?? nil,
        "locations": [["location":"idherrr"]]
    ]
    

    【讨论】:

    • 这么简单!谢谢@Sh_khan,这是你第二次帮助我了!
    【解决方案2】:

    你可以试试

    let parameters: Parameters = [
        "title": Event.title ?? nil,
        "locations": [dictionary]]
    ]
    

    【讨论】:

      【解决方案3】:

      如果您的 API 请求接受特定的字符串格式,那么您需要使用 DateFormatter 将日期格式转换为字符串格式,否则如果接受日期对象,则您传递但日期对象,但这可能是不可能的。所以请尝试使用由服务器参数预先确定的字符串格式的第一个选项转换日期。

      或者如果可能的话,在您的服务器上共享请求参数类型支持。

      【讨论】:

        猜你喜欢
        • 2019-06-20
        • 1970-01-01
        • 2018-09-05
        • 1970-01-01
        • 1970-01-01
        • 2017-11-16
        • 1970-01-01
        • 2019-06-03
        • 1970-01-01
        相关资源
        最近更新 更多