Reputation: 61
Hi guys I'm looking for a way to parse my XML to a few numbers of labels, the "XMLReader" class is where my XML to Dictionary converter is and which works fine for all my other views. and "getValue" is a method to remove the whitespaces and tabs. Now when i try to feed my labels with data i get this exception: NSArray unrecognized selector sent to 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>
Log:
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 ";
},
Upvotes: 0
Views: 465
Reputation: 1430
The line NSDictionary *statsdictionary = [XMLReader dictionaryForXMLString:statsdataAsString
error:&staterror];
is returning an NSArray, and not a NSDictionary, so the NSXMLParser crashes since an NSArray doesn't know any methods named object for key.
EDIT:
In your case, the last two lines needs to be changed to something like this (NB: UNTESTED!):
NSArray *statsArray = [XMLReader dictionaryForXMLString:statsdataAsString
error:&staterror];
_stCreditlabel.text = [[statsArray[0] valueForKey:@"Name"] valueForKey:@"text];
If you want to access more of the statistic
elements inside the statistics
array, you can loop them with:
for (NSDictionary *statsObject in statsArray){
}
Also, I'd recommend this tutorial on how to parse Xml in iOS: http://www.raywenderlich.com/725/xml-tutorial-for-ios-how-to-read-and-write-xml-documents-with-gdataxml.
Upvotes: 1