xcode – Alamorafire SwiftyJson FacebookSDK

前端之家收集整理的这篇文章主要介绍了xcode – Alamorafire SwiftyJson FacebookSDK前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用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
 }

不幸的是我收到此错误

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,error in
          print(userProfile) //This prints nil
          print(response) //This prints api error for expecting data
    }
}
原文链接:https://www.f2er.com/iOS/333917.html

猜你在找的iOS相关文章