Liron Shapira Liron Shapira - 1 year ago 74
Swift Question

Handling Facebook Graph API result in iOS SDK with Swift

I just want to request data from Facebook's Graph API, e.g. get the current user's basic info.

The Objective-C doc is:

[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {

/* My question: How do I read the contents of "result" in Swift? */

// Success! Include your code to handle the results here
NSLog(@"user info: %@", result);
} else {
// An error occurred, we need to handle the error
// See:

There's no Swift doc yet, and I'm confused about the "result" parameter whose type is "id".


It looks like result contains a dictionary, but it may be nil. In Swift, its type will map to AnyObject?.

So, in Swift, you could do something like:

// Cast result to optional dictionary type
let resultdict = result as? NSDictionary

if resultdict != nil {
    // Extract a value from the dictionary
    let idval = resultdict!["id"] as? String
    if idval != nil {
        println("the id is \(idval!)")

This can be simplified a bit:

let resultdict = result as? NSDictionary
if let idvalue = resultdict?["id"] as? String {
    println("the id value is \(idvalue)")