1

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

我只想用 key 提取所有元素name并将其存储在一个单独的NSMutableArray中,但我的 finalNSMutableArray *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'
4

2 回答 2

2

[[myArray objectAtIndex:i] objectForKey:@"name"];返回一个NSString对象而不是另一个数组,因此您不能向objectAtIndex它发送一个。检查数组的结构。

于 2012-04-04T15:24:28.063 回答
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]);
于 2012-04-04T16:53:37.813 回答