程序以NSException类型的未捕获异常终止

时间:2018-06-25 12:03:56

标签: objective-c xml xcode macos

我正在尝试浏览xml文件并显示内容

    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;

}

但是我在这一行中遇到了错误

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

错误提示 libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)

请帮助

2 个答案:

答案 0 :(得分:0)

检查 xml NSString是否不是

答案 1 :(得分:0)

从本地路径读取

organizations.<your region>.amazonaws.com

然后从本地路径使用

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"xml.xml"];

注意:NSXMLDocument是仅限OS X的类

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLDocument_Class/Reference/Reference.html

您不能在iOS中使用它。

在iOS上,您可以使用 NSXMLParser