Swift Facebook获取朋友图请求错误

时间:2016-07-23 22:58:37

标签: json swift xcode facebook facebook-graph-api

所以,我正在尝试获取我的应用程序上连接的所有其他Facebook好友的列表。

在完成FBSDKgraphrequest之后,我获得了正确的数据(见下文),然后将其转换为NSDictionary。数据如下所示:

{
    friends =     {
        data =         (
                        {
                id = 10154257515635281;
                name = "Hector Judd";
            },
                        {
                id = 151132798649181;
                name = "Arch Tester Dev";
            }
        );
        paging =         {
            cursors =             {
                after =  QVFIUndVRFZAINXhCSlAzdnNGeUUwTHdhamNpc3NFbjR2NjF4dk40N3ZAZAR2lNLXM0Q3BxLW90REVsaFk3aU13Um1Ca0NOd0ZAXN2gxaDF2emhYem9BMjhkMVl3;
                before = QVFIUlo2a1Q4UGhpWmY2SFNWbUtpMVcxZAnEtR01KSlUyeVEtMU9GbjNkRHp2bTFKY0VoVm5xX3dNLXEwMG5OY0Q0My0ZD;
            };
         };
        summary =         {
            "total_count" = 831;
        };
    };
    id = 10208811056967519;
}

然后尝试使用多个if语句解压缩它,但最终得到错误:分段错误11.请参阅下面的代码。

func getFriends() {

    let parameters = ["fields": "friends"]

    FBSDKGraphRequest(graphPath: "me", parameters: parameters).startWithCompletionHandler({ (connection, result, error) -> Void in

        if error != nil {

            print(error)

        } else {

            if let result = result as? NSDictionary {

                if let data = result["friends"] as? NSDictionary {

                    if let friendData = data["data"] as? NSArray {

                        for friend in friendData {

                            friendIds.append(friend["id"] as String)

                            print(friendIds)

                        }


                    }
                }

            }
        }

    })
}

我对编程比较陌生,并且正在努力找到一个好方法来做到这一点。我们欢迎所有的建议!!感谢。

1 个答案:

答案 0 :(得分:0)

尝试在您的Loop中将您的friendData变量声明为NSDictionary

   for friend in friends {
       if let friendData = friend as? NSDictionary {
              friendIds.append(friendData["id"] as! String)
       }
   }