0

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)");

    }
    }
slava
  • 1,901
  • 6
  • 28
  • 32
TheRapture87
  • 1,403
  • 3
  • 21
  • 31
  • From what I remember, you can only get a list of friends that are currently using the app - I'm not sure what the end point is for that. – Fred Faust Nov 16 '15 at 12:30
  • Yup you can only get friends which are using your app already. You are probably getting a nil array because maybe none of your friends are using that app – NSNoob Nov 16 '15 at 12:43

1 Answers1

2

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)");

      }
    }
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39