xcode - Alamorafire + SwiftyJson + FacebookSDK

标签 xcode swift facebook-ios-sdk

我使用 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
 }

不幸的是我收到这个错误:

Cast from 'JSON' to unrelated type '[String : AnyObject]' always fails

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

Cannot convert value of type 'JSON' to expected argument type '[String : AnyObject]?'

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

最佳答案

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
    }
}

关于xcode - Alamorafire + SwiftyJson + FacebookSDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663710/

相关文章:

ios - 如何关闭 FBWebDialogs

iphone - 媒体播放器框架 iOS SDK

ios - 如何防止调用 UIView.animate

ios - Swift 错误 : "Cannot find interface declaration for ' SKScene', 的父类(super class)”

ios - 两个 NSDate 之间的 Swift 天数

ios - 在 iOS 中使用 Facebook SDK 登录时无法获取用户的电子邮件 ID

ios - 没有缺口的 iPhone X 模拟器截图

ios - 我的导航栏的色调会自动改变

iphone - 如何在容器内执行推送转场