繁体   English   中英

NSXMLParser应用终止“无法识别的选择器发送到实例”

[英]NSXMLParser app terminating “unrecognized selector sent to instance”

嗨,大家好,我正在寻找一种将XML解析为几个标签的方法,“ XMLReader”类是我的XML到词典转换器的位置,并且对所有其他视图都适用。 “ getValue”是一种删除空格和制表符的方法。 现在,当我尝试用数据填充标签时,出现以下异常:NSArray无法识别的选择器发送到instance:0x....。

- (void)viewDidLoad
{
[super viewDidLoad];
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HHddMMyyyy"];
NSString* date_str = [formatter stringFromDate:date];
NSString *requestUrl = [NSString 
stringWithFormat:@"xyz",date_str];
NSURL *staturl= [NSURL URLWithString:requestUrl];
NSURLRequest *statrequest = [NSURLRequest requestWithURL:staturl];
NSURLResponse *statresponse;
NSError *staterror;
NSData * statdata = [NSURLConnection sendSynchronousRequest:statrequest 
returningResponse:&statresponse error:&staterror];
if (staterror) {
    NSLog(@"URL Error");
}
NSString *statsdataAsString = [[NSString alloc]initWithData:statdata 
encoding:NSASCIIStringEncoding];
NSDictionary *statsdictionary = [XMLReader dictionaryForXMLString:statsdataAsString 
error:&staterror];

_stCreditlabel.text =[XMLReader getValue:[[[[statsdictionary 
objectForKey:@"Statistics"]objectForKey:@"Statistics"]objectForKey:@"Statistic"]objectForKey:@"Name"]];

XML:

<Statistics xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:noNamespaceSchemaLocation="xyz">
   <Statistics>
    <Statistic>
     <Name>heute</Name>
    <Statistic>
      <Name></Name>
      <Value></Value>
    </Statistic>
    <Statistic>
     <Name></Name>
     <Value>518</Value>
    </Statistic>
   </Statistic>
  <Statistic>
   <Name>Allgemein</Name>
   <Statistic>
     <Name>Online</Name>
     <Value>2458</Value>
   </Statistic>
   <Statistic>
     <Name>Lager</Name>
     <Value>444</Value>
   </Statistic>     
  </Statistic>
 </Statistics>
</Statistics>

日志:

2014-04-15 11:04:22.282 MConsole[763:60b] *** Terminating app due to uncaught 
exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: 
unrecognized selector sent to instance 0x972ea70'
po 0x972ea70
<__NSArrayM 0x972ea70>(
{
Name =     {
    text = "\n  \n    \n      heute";
};
Statistic =     (
            {
        Name =             {
            text = "\n      \n       ";
        };
        Value =             {
            text = "\n        9";
        };
        text = "\n      ";
    },
            {
        Name =             {
            text = "\n      \n        ";
        };
        Value =             {
            text = "\n        518";
        };
        text = "\n      ";
    }
);
text = "\n    ";
},
{
Name =     {
    text = "\n    \n      Allgemein";
};
Statistic =     (
            {
        Name =             {
            text = "\n      \n        Online";
        };
        Value =             {
            text = "\n        2458";
        };
        text = "\n      ";
    },
            {
        Name =             {
            text = "\n      \n        Lager";
        };
        Value =             {
            text = "\n        444";
        };
        text = "\n      ";
    },

NSDictionary *statsdictionary = [XMLReader dictionaryForXMLString:statsdataAsString error:&staterror]; 返回一个NSArray,而不是NSDictionary,因此NSXMLParser崩溃,因为NSArray不知道任何名为key的object方法。

编辑:

对于您的情况,最后两行需要更改为以下内容(注意:未测试!):

NSArray *statsArray = [XMLReader dictionaryForXMLString:statsdataAsString 
error:&staterror];

_stCreditlabel.text = [[statsArray[0] valueForKey:@"Name"] valueForKey:@"text];

如果要访问statistic statistics数组内的更多statistic元素,可以使用以下方法循环它们:

for (NSDictionary *statsObject in statsArray){

}

另外,我建议本教程介绍如何在iOS中解析Xml: http : //www.raywenderlich.com/725/xml-tutorial-for-ios-how-to-read-and-write-xml-documents- with-gdataxml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于未捕获的异常&#39;NSInvalidArgumentException&#39;无法识别的选择器发送到实例而终止应用程序 终止应用程序-&#39;NSInvalidArgumentException&#39;,原因:&#39;-[NSCFString objectForKey:]:无法识别的选择器已发送到实例 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;-[__ NSArrayM objectForKey:]:无法识别的选择器已发送至实例 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:-[__ NSCFString方案]:无法识别的选择器已发送到实例 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;-[__ NSCFType next]:无法识别的选择器已发送至实例&#39; 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;-[__ NSCFString size]:无法识别的选择器已发送到实例 iOS-由于未捕获的异常&#39;NSInvalidArgumentException无法识别的选择器发送到实例0x7a9a2c0&#39;而终止应用程序 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;-[__ NSCFArray length]:无法识别的选择器已发送到实例 ***由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[__ NSDictionaryM名称]:无法识别的选择器已发送到实例 由于未捕获的异常&#39;NSInvalidArgumentException&#39;无法识别的选择器发送到实例Objective-C Singleton而终止应用程序
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM