Can't extract needed information from NSDictionary

余生颓废 提交于 2019-12-02 14:46:28

问题


I parse a XML file, and after parsing get a NSDictionary object (XMLDictionary named). I'm parsing this:

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

After that i'm trying this (titleArr - NSArray):

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

In Run-time I get this error in the above line: "Thread 1: signal SIGABRT". How can i fix this problem?


回答1:


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

At the very least, if it fails, the error will be isolated to a single operation. And the NSLogs will show you the values after each step.




回答2:


Try this:

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

Edited Answer

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


来源:https://stackoverflow.com/questions/19835181/cant-extract-needed-information-from-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!