使用Swift4 JSONDecoder解析IOS FBSDKGraphRequest结果

时间:2017-12-07 07:57:05

标签: ios swift facebook-graph-api swift4

FBSDKGraphRequest的结果属于任意类型,可以转换为NSDictionary,但如何将其转换为Data对象,以便能够使用此SWIFT4功能

let jsonDecoder = JSONDecoder.init()
let fbModel = try? jsonDecoder.decode(FBModel.self, from: dataFromResult)

我试试这个,但结果是代码注释为

    FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"email,name,id"]).start{ (connection, results, error) -> Void in
           let dataFromResult = NSKeyedArchiver.archivedData(withRootObject: results)
           let stringFromData = String.init(data: dataFromResult, encoding: String.Encoding.utf8)//return nil
           let jsonDecoder = JSONDecoder.init()
           let fbModel = try? jsonDecoder.decode(FBModel.self, from: dataFromResult)//fbModel nil
}

1 个答案:

答案 0 :(得分:0)

它对我有用,但我不确定它是否是最佳解决方案

let resDataFromDic = try? JSONSerialization.data(withJSONObject: resDict, options: JSONSerialization.WritingOptions.prettyPrinted)