0

I'm using MGTWitterEngine and I cannot figure out why my dictionary items are returning null.

I have this method:

- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier{
NSDictionary *result = [searchResults objectAtIndex:0];
NSString *fromUser = [result valueForKey:@"from_user"];
NSLog(@"from user: %@", fromUser);
}

And for some reason, my NSLog always displays "from user: NULL". I can do an NSLog of searchResults which dumps the contents of the search correctly, but I can't figure out how to parse the information. Any help would be greatly appreciated.

Bart
  • 19,692
  • 7
  • 68
  • 77
Ngoan Nguyen
  • 747
  • 2
  • 9
  • 19
  • 1
    NSLog displays null if the value is nil. NSDictionary returns nil if the key couldn't be found. –  Jun 04 '11 at 11:44

1 Answers1

1

Look at this question: Parsing Search Result with MGTwitterEngine in Objective C

They use:

- (void)searchResultsReceived:(NSArray *)searchResults 
                   forRequest:(NSString *)connectionIdentifier
{
    if ([searchResults count] > 0)
    {
        NSDictionary *result = [searchResults objectAtIndex:0];

        NSString *fromUser = [result valueForKey:@"from_user"];
        NSString *fromUserID = [result valueForKey@"from_user_id"];
        // ...
        NSString *text = [result valueForKey@"text"];

        NSLog(@"User %@(%@): %@", fromUser, fromUserID, text);
    }
}

It is similar to your code with a check on searchResults count.

Community
  • 1
  • 1
DreamOfMirrors
  • 2,147
  • 1
  • 21
  • 34
  • 1
    Yes, but I know for certain searchResults contains data. I can do an NSLog of it and it is an array with 1 entry of all search results. [searchResults count] is 1, and in my case it should always execute because I don't check for it's count. – Ngoan Nguyen Jun 04 '11 at 17:21
  • 1
    I figured it out, so stupid. My dictionary contained subItems and the key I was searching for was a subitem, not one of the main items. – Ngoan Nguyen Jun 04 '11 at 19:21
  • @Nguyen.. So… then **HOW** do you get to them, then? I've worked with plists and XML for years, yet these f-ing dictionaries are a headache to work with! – Alex Gray Dec 03 '11 at 01:38