TheRapture87
TheRapture87

Reputation: 1423

Facebook API retrieving friends list in swift

Why is this giving me a error?

fatal error: unexpectedly found nil while unwrapping an Optional value

Am I using the wrong valueForKey String? I want to get the list of friends. (There are friends because when I print result it shows them)

let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in

    if error == nil {
            let userName : NSArray = result.valueForKey("name") as! NSArray
        print("Friends are : \(result)")

    } else {

        print("Error Getting Friends \(error)");

    }
    }

Upvotes: 0

Views: 1792

Answers (1)

Tejas Ardeshna
Tejas Ardeshna

Reputation: 4371

Try this

let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in

    if error == nil {
       if let userNameArray : NSArray = result.valueForKey("data") as! NSArray
       {
          var i:Int = 0
          for i;i<userNameArray.count ; i++
          {
             print(userNameArray[i].valueForKey("name"))
          }      

       } else {

        print("Error Getting Friends \(error)");

      }
    }

Upvotes: 2

Related Questions