解析的.xml文件中的NSArray元素无法在iPhone上使用TouchXML访问

时间:2012-04-04 14:47:30

标签: objective-c ios nsarray touchxml

我有NSMutableArray *myArray,这是使用TouchXML正确.xml解析文件的结果。

我只想用密钥name提取所有元素,然后将其存储在单独的NSMutableArray中,但我的最终NSMutableArray *namesList无法访问,立即崩溃我的iPhone应用程序,因为它只包含最后一个枚举字符串,而不是整个数组。

以下是代码:

NSMutableArray *namesList = [[NSMutableArray alloc] initWithArray:myArray copyItems:YES];

       int i;
        for (i = 0; i < [myArray count]; i++) 
           {
            namesList = [[myArray objectAtIndex:i] objectForKey:@"name"];
            NSLog(@"All Names: %@", namesList);    
           }

          NSLog(@"First Name: %@", [namesList objectAtIndex:0]); <-- crashing line

这是NSLog:

-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a42540
2012-04-04 10:34:07.882 Sections[3610:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a42540'

2 个答案:

答案 0 :(得分:2)

[[myArray objectAtIndex:i] objectForKey:@"name"];会返回NSString个对象而不是其他数组,因此您无法向其发送objectAtIndex。检查阵列的结构。

答案 1 :(得分:2)

你应该可以这样做:

NSMutableArray * namesList = [NSMutableArray alloc] init];

for(i = 0; i < [myArray count]; i++)
{ 

    NSString * tempString = [NSString stringWithString:[[myArray objectAtIndex:i] objectForKey:@"name"]];
    [namesList addobject:tempString];
}

 NSLog(@"First Name: %@", [namesList objectAtIndex:0]);