首页 > 解决方案 > program terminating with uncaught exception of type NSException

问题描述

I am trying to go through an xml file and display the contents

    int main(int argc, const char * argv[])
    {
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"xml" ofType:@"xml"];
    NSString *xml = [NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:nil];
    NSXMLDocument *xmlDocument = [[NSXMLDocument alloc] initWithXMLString:xml options:0 error:nil];
    NSMutableArray *array = [NSMutableArray array];

    for (NSXMLElement *node in [xmlDocument.rootElement nodesForXPath:@"//app" error:nil])
    {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        NSMutableArray *photos = [NSMutableArray array];

        for (NSXMLElement *subNode in node.children)
        {
            if ([subNode.name isEqualToString:@"address"])
                [photos addObject:subNode.stringValue];
            else
                [dict setObject:subNode.stringValue forKey:subNode.name];
        }

        [dict setObject:photos forKey:@"address"];
        [array addObject:dict];
    }

    NSLog(@"%@", array);
    return 0;

}

But I'm getting an error in this line

NSXMLDocument *xmlDocument = [[NSXMLDocument alloc] initWithXMLString:xml options:0 error:nil];

The error says libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

please help

标签: objective-cxmlxcodemacos

解决方案


检查xml NSString 是否为 nil


推荐阅读