简体   繁体   English

使用Swift4 JSONDecoder解析IOS FBSDKGraphRequest结果

[英]parse IOS FBSDKGraphRequest result using Swift4 JSONDecoder

the result of FBSDKGraphRequest is of type any and could be casting to NSDictionary , but how to convert it to Data object to be able to use this SWIFT4 feature FBSDKGraphRequest的结果是任何类型,可以转换为NSDictionary ,但是如何将其转换为Data对象以能够使用此SWIFT4功能

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

i'm try this but the result was nil as code comments 我尝试这个,但是结果为零,因为代码注释

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM