Alamorafire + SwiftyJson + FacebookSDK

art*_*tuc 8 xcode facebook-ios-sdk swift

我使用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
 }
Run Code Online (Sandbox Code Playgroud)

不幸的是我收到此错误:

从'JSON'转换为不相关的类型'[String:AnyObject]'总是失败

如果我尝试将JSON数据直接发送到API,我会收到此错误:

无法将'JSON'类型的值转换为预期的参数类型'[String:AnyObject]?'

任何帮助表示赞赏.我只是想知道,如何将JSON对象转换为String AnyObject?没有失败.或者使用Swift 2将Json对象作为post/put/patch请求数据发送.

J.W*_*ang 1

JSON是在 中定义的自定义类型SwiftyJson。尝试在您的情况下使用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
    }
}
Run Code Online (Sandbox Code Playgroud)