【发布时间】:2016-06-10 09:02:09
【问题描述】:
我使用 FBSDK 和 Xcode 7.2.1 来检索用户配置文件信息并使用 SwiftyJson 来处理和操作 json 数据。更改值/删除一些值后,我想使用 JSON 数据向我的 api 发出发布请求。但是我在 swift 中遇到了非常糟糕的类型问题。
这是我发出帖子请求的代码:
let headers = [
"Content-Type": "application/json"
]
let userProfile = userProfile as? [String : AnyObject]
Alamofire.request(.POST, "http://localhost:8888/api/profile", parameters: userProfile, headers: headers, encoding:ParameterEncoding.URL) .response { request, response, data, error in
print(userProfile) //This prints nil
print(response) //This prints api error for expecting data
}
不幸的是,我收到了这个错误:
从“JSON”转换为不相关类型“[String : AnyObject]”总是失败
如果我尝试将 JSON 数据直接发送到 API,我会收到此错误:
无法将“JSON”类型的值转换为预期的参数类型“[String : AnyObject]?”
感谢任何帮助。我只想知道,如何将 JSON 对象转换为 String AnyObject?没有失败。或者使用 Swift 2 发送 Json 对象作为 post/put/patch 请求数据。
【问题讨论】:
-
尝试将编码从 .URL 更改为 .JSON
-
这不是问题。没用..
-
没问题可以分享userProfile的数据吗?
标签: xcode swift facebook-ios-sdk