Ajay_Kumar
Ajay_Kumar

Reputation: 1387

Retrieve separate values from an array

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?

Upvotes: 1

Views: 124

Answers (1)

user557219
user557219

Reputation:

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.

Upvotes: 3

Related Questions