1
votes

Im trying to use NSSortDescriptor to sort the NSMutableArray *friends, key:birthday, by date, I'm getting null for all the birthdays in my NSLog, however the names are still logging. If I pull out my NSSortDescriptor my NSLog will give me the values again, wondering why i lose the values in passing them through the NSSortDescriptor, and how I can pull this off. Im using the Hackbook sample code to learn Facebook integration, and have modified the code to give me my friends birthdays, and id like to have them sorted January - December in format 1/1 - 12/31 Is it because of the randomizing pull of resultData of a friends list? Thanks you in advance, ill be sure to accept as soon as we get to the bottom of this!

******************************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 edit added code above that shows how i am getting these names and birthdays, i believe them both to be NSStrings per http://developers.facebook.com/docs/reference/api/user/ Just scroll down to the birthday part. I want to also mention, VERY IMPORTANT, that i have extended permissions, and this is not a permission issue, its something im doing wrong when i try to sort the array, and i believe my inexperience is my own worst enemy here. Thanks!

1
There isn't anything obviously wrong with your code. Is birthday an NSDate object? - 0x141E
thats what killing me, i just cant figure out why it drops the values of "birthday" when running through that NSSortDescriptor, im going to post the code where i make the query as it involves NSMutDict. which why im probably dead wrong on this. - theProject
Code Monkey, I seen earlier that you had posted some code, but when i came back to view it, I dont see it, did you remove it? Thank u! *edit no i havent had any luck, now im stuck on a BOOL...maybe u can help there? lol - theProject

1 Answers

1
votes

This may help you identify the problem. In general, this allows you to sort a list with a custom comparator, but it may be useful to determine why sortUsingDescriptors is nulling your birthdays. Perhaps one or more of your birthdays is not valid.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                             initWithKey:@"birthday"
                               ascending:YES
                              comparator:^NSComparisonResult(id obj1, id obj2) {
                                  NSLog(@"bday1 = '%@' bday2 = '%@'", obj1, obj2);
                                   return NSOrderedSame;
                              }];