解析 Facebook 好友列表

Parse Facebook friends list

在统一api之前,我一直使用这段代码来解析Facebook的好友列表:

var list = new List<FacebookFriend>();
var graphObject = result as FBGraphObject;

using (var array = (NSMutableArray)graphObject[new NSString("data")])
{
    for (uint i = 0; i < array.Count; i++)
    {
        using (var friend = new NSDictionary(array.ValueAt (i)))
        {
            friends.Add (new FacebookFriend
            {
                ID = friend.ObjectForKey (new NSString ("uid")).ToString (),
                Name = friend.ObjectForKey (new NSString ("name")).ToString (),
            });
        }
    }
}

Facebook 好友:

public class FacebookFriend
    {

        public string ID { get; set; }
        public string name { get; set; }

    }

数组如下:

array {( { id = 100009010208340; name = "Patricia Amijajbjhcdj Wongsky"; }, { id = 100009036398265; name = "Patricia Amijcfcihbfe Vijayvergiyasen"; }, { id = 100009086915834; name = "Elizabeth Amijhfiaehcd Bharambesky" ; } )}

但是现在 NSDictionary 的保护级别改变了,这段代码被破坏了。 我试过使用一些工厂方法,但没有成功。

有谁知道在这种情况下应该使用什么而不是

new NSDictionary(array.ValueAt (i)))

谢谢

应该是这样的:

Runtime.GetNSObject<NSDictionary> (array.ValueAt (i));

旁注:在循环中创建相同的 NSString 常量效率不高。你应该这样做:

using (var uid = new NSString ("uid"))
using (var name = new NSString ("name")) {
    for (uint i = 0; i < array.Count; i++) {
       using (var friend = Runtime.GetNSObject<NSDictionary> (array.ValueAt (i))) {
        friends.Add (new FacebookFriend
        {
            ID = friend.ObjectForKey (uid).ToString (),
            Name = friend.ObjectForKey (name).ToString (),
        });
    }
}

我编译了答案并添加了一个可能对某人有帮助的 get,因为我花了几天时间弄清楚如何使这个请求在统一的 API 和 Facebook SDK V2.2 上工作Xamarin IOS.

List<string> mUserFriendsList = new List<string>();
    if(FBSession.ActiveSession.IsOpen)
                    {
                        var dic = new NSDictionary();
                        FBRequestConnection.StartWithGraphPath("me/friends?fields=id",dic,"GET",(connection,result,error) =>
                        {
                            if(error != null){
                                Console.WriteLine(error.Description);
                                InvokeOnMainThread (() => new UIAlertView ("Error!", "An error has occured while trying to connect to facebook. Please try again later.", null, "Ok", null).Show ());
                            }else{
                                var graphObject = result as FBGraphObject;
                                using (var array = (NSMutableArray)graphObject[new NSString("data")])
                                {
                                     using(var id = new NSString("id"));
                                     for (uint i = 0; i < array.Count; i++) 
                                     {
                                        using (var friend = Runtime.GetNSObject<NSDictionary> (array.ValueAt (i))) 
                                        {
                                            mUserFriend = friend["id"].ToString ();
                                            mUserFriendsList.Add (mUserFriend);
                                        }
                                    }
                                }
                            }
                        });
                    }