1

我试图使用 NSSortDescriptor 对 NSMutableArray *friends, key:birthday, 按日期进行排序,我的 NSLog 中的所有生日都为空,但名称仍在记录中。如果我拔出我的 NSSortDescriptor,我的 NSLog 将再次给我这些值,想知道为什么我在将它们传递给 NSSortDescriptor 时会丢失这些值,以及我如何才能完成它。我使用 Hackbook 示例代码来学习 Facebook 集成,并修改了代码给我朋友的生日,并且我想让他们按照 1/1 - 12/31 的格式对 1 月 - 12 月进行排序是不是因为随机拉动朋友列表的结果数据?提前感谢您,一旦我们查明真相,我一定会接受!

******************************CODE TO PULL KEYS BIRTHDAY AND NAMES FROM GRAPH API ******

   - (void)getUserFriends {
currentAPICall = kAPIGraphUserFriends;
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication]      delegate];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            @"name, birthday",  @"fields", nil];
[[delegate facebook] requestWithGraphPath:@"me/friends" andParams:params andDelegate:self];
[self apiGraphFriends];
}


  ***********RANDOMIZE LIST OF FRIENDS NAMES W BIRTHDAYS AND PUSH TO VIEW CONTROLLER

NSMutableArray *friends = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *resultData = [result objectForKey:@"data"];
if ([resultData count] > 0) {
    for (NSUInteger i=0; i<[resultData count] && i < 1000; i++) {
        [friends addObject:[resultData objectAtIndex:i]];
    }
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthday"
                                                                   ascending:TRUE];
    [friends sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];

    // LOG VALUES OF KEYS birthday and name FOR DEBUGGING

    NSLog(@"name %@" , [friends valueForKey:@"name"]);
    NSLog(@"birthday %@" , [friends valueForKey:@"birthday"]);

    // Show the friend information in a new view controller
    NSLog(@"Check#3");
    APIResultsViewController *controller = [[APIResultsViewController alloc]
                                                initWithTitle:@"Friends Birthdays"
                                                         data:friends action:@""];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
} else {
    [self showMessage:@"You have no friends."];
}
[friends release];

8/10 编辑在上面添加了代码,显示了我如何获得这些名字和生日,我相信它们都是http://developers.facebook.com/docs/reference/api/user/的NSStrings 只需向下滚动到生日部分。我还想提一下,非常重要的是,我已经扩展了权限,这不是权限问题,当我尝试对数组进行排序时,我做错了,我相信我的经验不足是我自己最大的敌人。谢谢!

4

1 回答 1

1

这可以帮助您识别问题。通常,这允许您使用自定义比较器对列表进行排序,但确定 sortUsingDescriptors 将您的生日归零的原因可能很有用。也许您的一个或多个生日无效。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                             initWithKey:@"birthday"
                               ascending:YES
                              comparator:^NSComparisonResult(id obj1, id obj2) {
                                  NSLog(@"bday1 = '%@' bday2 = '%@'", obj1, obj2);
                                   return NSOrderedSame;
                              }];
于 2012-08-11T01:12:18.697 回答