Displaying an Array of Objects
Trying to populate a uitable from xml, xml is already parsed.
for (Row* fighter in parser.fighter)
{
NSLog(@"%@",fighter.title);
NSLog(@"%@",fighter.info);
NSLog(@"%@",fighter.img);
NSLog(@"=========");
}
prints out:开发者_如何学运维
title0
info0
img0
=========
title1
info1
img1
=========
title2
info2
img2
=========
title3
info3
img3
=========
I'm trying to put the parsed data into an array that has 2 columns
ex. [[figher0, fighter1] , [fighter2, fighter3]]
NSMutableArray *holderArray = [NSMutableArray array];
for (NSInteger i = 1; i < [parser.fighter count]; i+=2) {
id object1 = [parser.fighter objectAtIndex:i-1];
id object2 = [parser.fighter objectAtIndex:i];
[holderArray addObject:[NSArray arrayWithObjects:object1, object2, nil]];
}
for (int j=0; j< [holderArray count]; j++) {
NSLog(@"%d :: %@",j, [holderArray objectAtIndex:j]);
NSLog(@"=====",);
}
prints out:
"<Row: 0x600c5f0>",
"<Row: 0x600c680>"
=========
"<Row: 0x600c640>",
"<Row: 0x6106f60>"
=========
"<Row: 0x600ee90>",
"<Row: 0x600eed0>"
=========
"<Row: 0x600ec60>",
"<Row: 0x600f0f0>"
=========
How do i access the data ie figher.title, fighter.info and fighter.img?
thanks
Try Using: -
NSMutableArray *holderArray = [NSMutableArray array];
for (NSInteger i = 1; i < [parser.fighter count]; i+=2) {
id object1 = [parser.fighter objectAtIndex:i-1];
id object2 = [parser.fighter objectAtIndex:i];
[holderArray addObject:[NSArray arrayWithObjects:object1, object2, nil]];
}
for (int j=0; j< [holderArray count]; j++) {
NSLog(@"%@ :: %@",[holderArray objectAtIndex:j].title, [holderArray objectAtIndex:j].info);
NSLog(@"=====",);
}
I think you need to do this
for (int j=0; j< [holderArray count]; j++) {
NSLog(@"%d :: title = %@, info = %@",j, [holderArray objectAtIndex:j].title, [holderArray objectAtIndex:j].info);
NSLog(@"=====",);
}
In your last loop you are trying to print an array of two fighters. The array is only printed as a hex pointer. What you should do is to retrieve the elements of your inner array and collect them in a string:
for (int j=0; j< [rootArray count]; j++) {
NSMutableString rowString = [NSMutableString stringWithCapacity:100];
[rowString appendFormat:@"[ "];
for( Row* fighter in [rootArray objectAtIndex:j] ) {
[rowString appendFormat:@"[%@, %@] ", fighter.title, fighter.info];
}
[rowString appendFormat:@"]"];
NSLog(@"%d :: %@",j, rowString);
if( j > 0 && j % 2 == 0 ) {
NSLog(@"=====");
}
}
I have no compiler at hand so please check for syntax errors in my example by yourself ;-).
精彩评论