1

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?

2 Answers 2

4

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.