显示对象数组

时间:2011-02-17 15:14:30

标签: iphone objective-c ios4

尝试从xml填充uitable,xml已经被解析。

for (Row* fighter in parser.fighter) 
{   
    NSLog(@"%@",fighter.title);
    NSLog(@"%@",fighter.info);
    NSLog(@"%@",fighter.img);
    NSLog(@"=========");
}

打印出来:

title0
info0
img0
=========
title1
info1
img1
=========
title2
info2
img2
=========
title3
info3
img3
=========

我正在尝试将解析后的数据放入一个包含2列的数组

离。 [[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(@"=====",);   
}

打印出来:

"<Row: 0x600c5f0>",
"<Row: 0x600c680>"
=========
"<Row: 0x600c640>",
"<Row: 0x6106f60>"
=========
"<Row: 0x600ee90>",
"<Row: 0x600eed0>"
=========
"<Row: 0x600ec60>",
"<Row: 0x600f0f0>"
=========

如何访问数据,即figher.title,fighter.info和fighter.img?

感谢

3 个答案:

答案 0 :(得分:0)

在你的最后一个循环中,你正在尝试打印两个战士阵列。该数组仅作为十六进制指针打印。你应该做的是检索内部数组的元素并将它们收集在一个字符串中:

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(@"=====");   
    }
}

我手边没有编译器,所以请自行检查我的示例中的语法错误; - )。

答案 1 :(得分:0)

尝试使用: -

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(@"=====",);   
}

答案 2 :(得分:0)

我认为你需要这样做

for (int j=0; j< [holderArray count]; j++) {
    NSLog(@"%d :: title = %@, info = %@",j, [holderArray objectAtIndex:j].title, [holderArray objectAtIndex:j].info);
    NSLog(@"=====",);   
}