views:

57

answers:

2

I have the following array, which was retrieved from JSON results:

<NSCFArray 0x196e3e0>(  
{  
NameID = 3;  
Name = test1;  
},  
{  
NameID = 6;  
Name = test2;  
}  
)

I'd like to go through each object via a tableview and assign its values to labels in a custom tableview cell. How do I access NameID and Name on each iteration? Do I need to create a class with those two properties and assign to it from the array?

+1  A: 

It's an array of NSDictionary so just iterate through the array and cast to NSDictionary then ask for the key to get the value.

CiNN
+4  A: 

Assuming the array is something like:

NSArray * array = [NSArray arrayWithObjects:
    [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:3], @"NameID", @"test1", @"Name", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:6], @"NameID", @"test2", @"Name", nil],
    nil];

You can iterate it like this:

NSEnumerator *enumerator = [array objectEnumerator];
id obj;
while ((obj = [enumerator nextObject]))
{
    NSLog(@"NameID:[%@]; Name:[%@]",
        [obj objectForKey:@"NameID"],
        [obj objectForKey:@"Name"]);
}

Output:

2010-02-04 11:41:53.266 x[8739] NameID:[3]; Name:[test1]
2010-02-04 11:41:53.267 x[8739] NameID:[6]; Name:[test2]
stefanB