ios - 带有 SwiftyJSON 参数的 Alamofire 请求

标签 ios swift alamofire swifty-json

我想知道如何将 SwiftyJSON 数组放入 Alamofire 请求的参数中。

实际上我使用这段代码 (Swift 3) 来设置请求的参数:

let params = ["customObjects": customObjects.map({$0.toJSON()})]

...但是如果我尝试启动请求,这会引发异常:

uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'

“customObjects”是我的自定义模型类的数组。

如果您需要更多信息,请告诉我。

最佳答案

您可以将 SwiftyJSON 对象(此处称为 yourJSON)转换为如下参数:

let parameters : Parameters = yourJSON.dictionaryObject ?? [:]

然后你可以像这样在 Alamofire 请求中使用它:

Alamofire.request("https://yourURL.com/somePath", method: .post, parameters: parameters, encoding:  JSONEncoding.default, headers: nil).responseJSON { response in
   // ... do whatever you would like with the response 
}

关于ios - 带有 SwiftyJSON 参数的 Alamofire 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45039814/

相关文章:

ios - CloudKit 订阅和 UNNotificationServiceExtension

ios - 我怎样才能找到给定日期的格林威治标准时间

swift - 为什么 UISearchBar 在 Swift 中跳下并覆盖/重叠第一行?

ios - Alamofire 4.0 的证书无效问题

swift - 永久设置 alamofire cookie

ios - 使用 KVC 访问私有(private)属性(以下划线开头)是否被视为使用私有(private) API?

ios - bool 值 : EnableTransactions 的 Flutter 未知键

ios - 在 Playground 中使用实况照片

swift - 类型开关模式

ios - 如何在 Xcode 10.1 中添加 Alamofire