FBSDKGraphRequest的结果是any类型,可以转换为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
}

最佳答案

它为我工作,但我不确定这是否是最佳解决方案

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

10-08 01:01