NSDictionary length :unrecognized selector sent to

2019-08-02 18:25发布

Hi guys I'm trying to parse some XML data to my tableview and get this failure: unrecognized selector sent to instance. I have an XMLReader class which is used for converting XML to NSDictionary which i have from this site: http://ios.biomsoft.com/2011/09/11/simple-xml-to-nsdictionary-converter/ how can I get app work?

- (void)viewDidLoad
{
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"yxz"];
//get content of url
NSURLRequest* request=[NSURLRequest requestWithURL:url];
NSURLResponse*response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
    NSLog(@"ERROR: %@",error);
}

// NSLog(@"data: = %@",data);
NSString* dataAsString =[[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
//NSLog(@"dataAsString= %@",dataAsString);
//parse content
feeds = [[NSMutableArray alloc]init];
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:dataAsString error:&error];
if(xmlDictionary==nil){NSLog(@"ERROR: Dictionary is NULL");}
else{
    if ([xmlDictionary objectForKey:@"OrderList"]==nil) {NSLog(@"OrderList not found");}
        else{
            if([[xmlDictionary objectForKey:@"OrderList"]objectForKey:@"Order"]==nil) {NSLog(@"No Orders");}
            else
            {
                feeds = [[xmlDictionary objectForKey:@"OrderList"]objectForKey:@"Order"];
            }

        }
    }
NSLog(@"XMLDictionary: %@",xmlDictionary);
}

The XML example(deleted the values which are not important anyway)

<?xml version="1.0" encoding="ISO-8859-1"?>
<OrderList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xsi:noNamespaceSchemaLocation="http://xyz/schema/OrderListSchema.xsd">
  <Order>
    <ID></ID>
    <Name></Name>
    <Payment></Payment>
    <Marge></Marge>
    <CountryISO2></CountryISO2>
    <Status ></Status>
  </Order>

2014-04-09 16:42:16.786 djisjdk[714:60b] -[__NSDictionaryI length]: unrecognized 
selector sent to instance 0x8c46d10
(lldb) bt
* thread #1: tid = 0x287a, 0x015718b9 libobjc.A.dylib`objc_exception_throw, queue = 
'com.apple.main-thread', stop reason = breakpoint 1.3
frame #0: 0x015718b9 libobjc.A.dylib`objc_exception_throw

标签: ios xml parsing
1条回答
戒情不戒烟
2楼-- · 2019-08-02 18:41

-[__NSDictionaryI length]: unrecognized selector sent to instance

This message tells you that NSDictionary (or, specifically, the private immutable subclass __NSDictionaryI) received a call to the length method, which it doesn't respond to.

'length' is a common method to call on a string, so it's a fairly safe bet that that is what the code which threw the exception was expecting. NSData also has a length method, but this seems less likely based on your code.

You need to step through the code to find where you have a dictionary but the code expects a string (or data).

查看更多
登录 后发表回答