Setting cell.textLabel.text with NSDictionary



如何引用我在下面创建的NSDictionary *fbFriends

[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (!error) {
                    NSDictionary<FBGraphUser> *me = (NSDictionary<FBGraphUser> *)result;
                    // Store the Facebook Id
                    [[PFUser currentUser] setObject:me.objectID forKey:@"fbId"];
                    [[PFUser currentUser] saveInBackground];
                    // 1
                    FBRequest *friendsRequest = [FBRequest requestForGraphPath:@"/me/friends"];
                    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                                  NSDictionary* result,
                                                                  NSError *error) {
                        // 2
                        NSArray *friends = result[@"data"];
                        for (NSDictionary<FBGraphUser>* friend in friends) {
                            NSLog(@"Found a friend: %@", friend.name);
                            // 3
                            // Add the friend to the list of friends in the DataStore
                            [[DataStore instance].fbFriends setObject:friend forKey:friend.objectID];
                        }
                        // 4
                        // Callback - login successful
                        if ([delegate respondsToSelector:@selector(commsDidLogin:)]) {
                            [delegate commsDidLogin:YES];
                        }
                    }];

                    // Add the User to the list of friends in the DataStore
                    [[DataStore instance].fbFriends setObject:me forKey:me.objectID];
                }
            }];

在DataStore.h中有

@property (nonatomic, strong) NSMutableDictionary *fbFriends;

我一直试图引用它的几种方式,但没有工作。我不知道该用哪把钥匙在我的tableviewcontroller

cell.textLabel.text=[[DataStore instance].fbFriends objectForKey:self];

我想列出你的fb好友也使用这个应用程序的名字。我的NSLog显示我有朋友,但我不知道如何在表视图控制器单元格

中显示它们

我使用的代码来自Ray Wenderlichs教程http://www.raywenderlich.com/44833/integrating-facebook-and-parse-tutorial-part-2

这是字典的NSLog

Dictionary contents: {
    10152183401605823 =     {
        id = 10152183401605823;
        name = "John Gor";
    };
    10203978226397607 =     {
        "first_name" = Katie;
        gender = female;
        id = 10237890226397607;
        "last_name" = James;
        link = "https://www.facebook.com/app_scoped_user_id/10237890226397607/";
        locale = "en_US";
        name = "Katie James";
        timezone = "-5";
        "updated_time" = "2013-12-22T16:18:07+0000";
        verified = 1;
    };

几点:

您发布的代码调用[[DataStore instance].fbFriends setObject: <some_value>];

只有在您创建了一个空字典并在某个时候将其保存在数据存储的fbFriends属性中时才会起作用。声明属性是不够的,你必须在某个地方分配一个空字典,使用new、dictionaryWithCapacity、alloc/init或类似的方法。

这个代码:

cell.textLabel.text=[[DataStore instance].fbFriends objectForKey:self];

没有意义。为什么要使用"self"作为fbFriends数组的键呢?Self大概是视图控制器。使用视图控制器作为字典的键是错误的。

你希望在字典中找到哪些键/值对?

在获取键/值对之前,你必须知道字典中存储了什么数据。你把字典里的内容记下来了吗?代码可能看起来像这样:

NSLog(@"Dictionary contents: %@", [DataStore instance].fbFriends);

在我看来,fbFriends是一个朋友对象(也是字典)的字典,以朋友ID作为fbFriends中每个条目的键来存储。因此,即使你能够从fbFriends中加载一个好友对象,你也不能从fbFriends中为你的标签文本分配一个条目。

你想在表视图的标签文本中显示什么?

而且,你的fbFriends是一本字典。字典不是有序的,所以它们不适合提供表视图。

最新更新