Alamorafire + SwiftyJson + FacebookSDK

时间:2016-02-26 23:38:26

标签: xcode swift facebook-ios-sdk

我使用FBSDK和Xcode 7.2.1来检索用户配置文件信息,并使用SwiftyJson来处理和操作json数据。更改值/删除一些后,我想使用JSON数据向我的api发布请求。但是我很快就遇到了非常糟糕的类型问题。

这是我发布帖子请求的代码:

 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请求数据发送。

1 个答案:

答案 0 :(得分:1)

JSONSwiftyJson中定义的自定义类型。尝试在您的案例中使用userProfile.dictionaryObject来获取基础swift Dictionary

通过此

打开可选字典
if let userProfile = userProfile.dictionaryObject {
    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
    }
}
相关问题