【问题标题】:NSDictionary length :unrecognized selector sent to instanceNSDictionary 长度:发送到实例的无法识别的选择器
【发布时间】:2014-05-22 21:32:03
【问题描述】:

大家好,我正在尝试将一些 XML 数据解析到我的 tableview 并出现此错误:无法识别的选择器已发送到实例。我有一个 XMLReader 类,用于将 XML 转换为我从这个站点获得的 NSDictionary: http://ios.biomsoft.com/2011/09/11/simple-xml-to-nsdictionary-converter/ 我怎样才能让应用工作?

- (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);
}

XML 示例(删除不重要的值)

<?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

【问题讨论】:

  • 给出完整的异常消息和堆栈跟踪 - 它来自哪一行?
  • 你是这个意思吗?我在代码下添加了它
  • 这并没有告诉我是哪一行。我知道当代码需要一个字符串时你正在使用字典,但你需要知道在哪里 - 哪个字典 - 在你可以修复它之前......
  • 我在 All Exceptions 中设置了一个断点,它在主文件中的这一行停止:@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }
  • 我不知道,我唯一拥有的字典是上面的 XMLDictionary (NSDictionary) 和另一个称为 order 的 NSMutableDictionary。

标签: ios xml parsing


【解决方案1】:

-[__NSDictionaryI 长度]:无法识别 选择器发送到实例

此消息告诉您NSDictionary(或者,具体来说,私有不可变子类__NSDictionaryI)收到了对length 方法的调用,但它没有响应。

'length' 是调用字符串的常用方法,因此可以肯定的是,这就是引发异常的代码所期望的。 NSData 也有一个 length 方法,但根据您的代码,这似乎不太可能。

您需要单步执行代码才能找到字典所在的位置,但代码需要一个字符串(或数据)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多