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

标签 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'

最佳答案

您应该可以执行以下操作:

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]);

关于objective-c - 使用TouchXML无法在iPhone上访问已解析的.xml文件中的NSArray元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10013570/

相关文章:

ios - 快速将多个独立项目居中

ios - 无法获得 supportedInterfaceOrientationsForWindow 以使用 Swift 1.2

ios - 由于空数组导致应用程序崩溃

objective-c - 对实体数组进行迭代,并根据 "other key"匹配的值获取某个键的值

iphone - 如何绘制矩形?

objective-c - 如何在 Cocoa 程序中将 View 滑入和滑出窗口

iphone - "UIWindowLevelStatusBar + 1"奇怪的键盘行为

ios - 从字典数组映射到 Swift 中的数字数组

iphone - 关于使用 RegexKitLite 进行正则表达式评估的问题

ios - 如何在 iOS 中旋转设备后使 UIButton 居中