gpt4 book ai didi

swift - Swift 3中如何处理FBSDKGraphRequest的响应数据

转载 作者:IT王子 更新时间:2023-10-29 05:23:23 28 4
gpt4 key购买 nike

我已经集成了 FB 最新的 SDK(非 swift)并且登录工作正常。我只需要知道如何解析图形响应数据,因为它不是有效的 JSON

工作代码:

     func configureFacebook()
{
login.readPermissions = ["public_profile", "email", "user_friends"];
login.delegate = self
}


func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
print("Login buttoon clicked")
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"])

graphRequest.start(completionHandler: { (connection, result, error) -> Void in

if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print(result)

}
})
}

输出:

Login button clicked
Optional({
"first_name" = KD;
id = 10154CXXX;
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/XXXn.jpg?oh=a75a5c1b868fa63XXX146A";
};
};
})

那么我应该将上述数据转换成什么格式来获取 URL 或 first_name 等值?

我也尝试转换为 NSDictionary 并得到错误:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
print("Login buttoon clicked")
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"])

graphRequest.start(completionHandler: { (connection, result, error) -> Void in

if ((error) != nil)
{
print("Error: \(error)")
}
else
{
do {

let fbResult = try JSONSerialization.jsonObject(with: result as! Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
print(fbResult.value(forKey: "name"))


} catch {
print(error)
}

}
})
}

最佳答案

您可以简单地按照以下方式进行操作:

let data:[String:AnyObject] = result as! [String : AnyObject]
print(data["first_name"]!)

swift 3:

安全解包 & Any 而不是 AnyObject

if let data = result as? [String:Any] {

}

关于swift - Swift 3中如何处理FBSDKGraphRequest的响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39384238/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com