对FBSDKGraphRequest(FBSDK v 4.1.0)的结果进行类型化访问

时间:2015-05-27 04:18:26

标签: ios facebook swift

我想知道是否存在对从FBSDKGraphRequest返回的结果的类型化访问权限?

例如,我按如下方式请求我自己的个人资料:

var theRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)

theRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
            // GET TYPED ACCESS TO MY ID
            println(result.id)
            // GET TYPED ACCESS TO MY GENDER
            println(result.gender)
        }
    )

这不起作用,因为结果当前是AnyObject类型,AnyObject没有返回信息的字段。基本上我想知道是否有一个类/协议,我可以转换结果,以便我有类型访问。这可能吗?看起来它不是我迄今为止研究过的。

1 个答案:

答案 0 :(得分:5)

创建字典:

class ViewController: UIViewController {
    var dict : NSDictionary!
}

获取数据:

if((FBSDKAccessToken.currentAccessToken()) != nil){
    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
        if (error == nil){
            self.dict = result as NSDictionary               
            println(self.dict)
            NSLog(self.dict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String)
        }
    })
}

输出应为:

{
    email = "ashishkakkad8@gmail.com";
    "first_name" = Ashish;
    id = 910855688971343;
    "last_name" = Kakkad;
    name = "Ashish Kakkad";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/s200x200/22501_915701971820048_9046303472199214595_n.jpg?oh=f3b3564f1450c13332b3067a135cad5d&oe=55C71792&__gda__=1443571904_c4667dcb08d85682edfd77a90ee9c3ab";
        };
    };
}
2015-05-25 22:12:34.015 SwiftFB[2713:7830] https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/s200x200/22501_915701971820048_9046303472199214595_n.jpg?oh=f3b3564f1450c13332b3067a135cad5d&oe=55C71792&__gda__=1443571904_c4667dcb08d85682edfd77a90ee9c3ab