开发者

How to represent object from array?

开发者 https://www.devze.com 2022-12-19 06:43 出处:网络
I have the following array, which was retrieved from JSON results: <NSCFArray 0x196e3e0>( { NameID = 3;开发者_如何学Go

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

<NSCFArray 0x196e3e0>(  
{  
NameID = 3;  开发者_如何学Go
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?


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]


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消