【问题标题】:Alamofire 'invalid type in JSON write' error after upgrading升级后 Alamofire“JSON 写入中的类型无效”错误
【发布时间】:2016-09-27 15:53:43
【问题描述】:

以下代码适用于 Alamofire 3.0:

let headers = [
    "content-type": "application/json",
    "cache-control": "no-cache",
    ]
let parameters = [
    "access_token": token.tokenString,
    "client_id": bundleID,
]

Alamofire.request(.POST, url, headers: headers, parameters: parameters, encoding: .JSON).responseJSON { response in
    // ...
}

我在更新到 Alamofire 4.0/Swift3/XCode 8 后更改了请求:

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
    // ...
}

我得到了错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'

我一直在寻找解决此问题的方法,但找不到任何相关内容。

【问题讨论】:

  • 我认为token.tokenStringbundleID 不是您所期望的字符串。

标签: ios json swift alamofire


【解决方案1】:

您的参数肯定有问题。确保token.tokenStringbundleID 都是Strings。

【讨论】:

  • 你说得对,token.tokenString 的类型是 String?,并且显式展开它可以解决问题。我不敢相信它是如此简单!错误消息对我来说非常不清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 2016-10-28
  • 1970-01-01
  • 2017-01-27
  • 2016-03-16
  • 2017-03-05
相关资源
最近更新 更多