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
}
答案 0 :(得分:0)
它对我有用,但我不确定它是否是最佳解决方案
let resDataFromDic = try? JSONSerialization.data(withJSONObject: resDict, options: JSONSerialization.WritingOptions.prettyPrinted)