无法从NSDictionary中提取所需信息

时间:2013-11-07 11:50:52

标签: ios objective-c nsdictionary

我解析一个XML文件,并在解析后得到一个NSDictionary对象(XMLDictionary命名)。我正在解析这个:

<result><node><id>27</id><name>Name 1</name><type>0</type><price>0</price><img>/upload/iblock/1a1/1a138b2d4da6e42398beeb21acc8e84f.png</img></node><node><id>28</id><name>Name 2</name><type>0</type><price>0</price><img>/upload/iblock/b72/b724d1550f93987a73b04974b5f7390e.png</img></node></result>

之后我正在尝试这个(titleArr - NSArray):

_titleArr = [[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"node"] objectForKey:@"name"] valueForKey:@"text"];

在运行时我在上面的行中得到了这个错误:“Thread 1:signal SIGABRT”。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

试试这个:

NSArray *arrResult = [_xmlDictionary objectForKey:@"result"];
for(int i = 0; i < [arrResult count]; i++)
{
    NSArray *arrNode = [arrResult objectAtIndex:i] valueForKey:@"node"];
    NSString *sName = [arrNode valueForKey:@"Name"];
    NSLog(@"Name : %@",sName);
}

已编辑的答案

  NSArray *arrResult = [_xmlDictionary valueForKey:@"result"];
    for (int i = 0; i < [arrResult count]; i++) {
        NSDictionary *dicNode = [[arrResult objectAtIndex:i] valueForKey:@"Node"];
        NSString *sName = [dicNode valueForKey:@"Name"];
        NSLog(@"Name : %@",sName);
    }

答案 1 :(得分:0)

NSError* parseError = nil;
_xmlDictionary = [XMLReader dictionaryForXMLString:myXMLString error:&parseError];
if (parseError != nil) {
    NSLog(@"Error on XML parse: %@", parseError);
}

NSLog(@"The full dictionary is %@", _xmlDictionary);

NSDictionary* result = [_xmlDictionary objectForKey:@"result"];
NSLog(@"'result' = %@", result);
NSDictionary* node = [result objectForKey:@"node"];
NSLog(@"'node' = %@", node);
NSString* name = [node objectForKey:@"name"];
NSLog(@"'name' = %@", name);

至少,如果失败,错误将被隔离到单个操作。 NSLogs会在每一步后显示值。