1

I have a variable which is printing its value on console like this:

source id key is (

   {

   name = "ABC Rawal";

   uid = 10000048171452;

} )

This is the code I'm using to do that:

-(void)request:(FBRequest*)request didLoad:(id)result {

    NSLog(@"source id key is %@", result);

}

I have to get name and uid separately. How can I retrieve those value from result?

3
  • 2
    we can't know for sure since you are not showing the definition of the class of result. It could be a dictionary in that case it would be: NSString *name = [result objectForKey:@"name"]; and int uid = [[result objectForKey:@"uid"] intValue]; but I am just guessing. Commented Jun 21, 2011 at 6:09
  • Inside of function there is one statement like NSArray* users = result; so I think result is NSArray variable. Commented Jun 21, 2011 at 6:18
  • @nacho The output is either a string (unlikely) or an array. Notice the parentheses — they’re the NeXTSTEP representation of an array, and the one used by -[NSArray description]. Commented Jun 21, 2011 at 6:21

1 Answer 1

3

result is an array comprised of dictionaries. In your example, the array has only one element.

If you want only the first element in the array, then:

if ([result count] > 0) {
    NSDictionary *person = [result objectAtIndex:0];
    NSString *personName = [person objectForKey:@"name"];
    NSString *personUID = [person objectForKey:@"uid"];
    …
}

Or, if you want to iterate over all elements in the array — in case the array has more than one element — then:

for (NSDictionary *person in result) {
    NSString *personName = [person objectForKey:@"name"];
    NSString *personUID = [person objectForKey:@"uid"];
    …    
}

Note that from your output it’s not possible to know whether uid is a string or a number. The code above considers it’s a string; in case it’s a number, use:

NSInteger personUID = [[person objectForKey:@"uid"] integerValue];

or an appropriate integer type that can hold the range of possible values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.