首页 > 解决方案 > 如何从不同变量的结果输出中获取电子邮件、姓名和图像

问题描述

 func getFBUserData(){
        if((FBSDKAccessToken.current()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
                if (error == nil){
                    //everything works print the user data
                    print(result)                                    
                }
            })
        }
 }

输出是:

可选({电子邮件=“rajput.abhishek660@gmail.com”;“first_name”= Abhishek;id = 1760800260695143;“last_name”= Rajput;名称=“Abhishek Rajput”;图片={数据={高度=200;“is_silhouette " = 0; url = "platform-lookaside.fbsbx.com/platform/profilepic/…;; 宽度 = 200; }; }; })

标签: iosswiftxcode

解决方案


你可以试试

if let res = result as? [String:Any] {
       print(res["first_name"])
       print(res["last_name"])
       print(res["name"])
       print(res["email"]) 
       if let picture = res["picture"] as? [String:Any] {                    
           if let data = picture["data"] as? [String:Any] {
                print(data["url"])
           } 
       }
}

你也可以使用Codable


推荐阅读