Hey everbody. How can i handle a error with the TBXML? I mean, if dont find the file, or what else. Actually, my app just crashes when i turn off the server where the file is.
So, how can i handle that?
- (void)viewDidLoad {
TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://localhost/dev/mcomm/produto.xml"]] retain];
TBXMLElement * rootXMLElement = tbxml.rootXMLElement;
}
Thanks!
If the error’s coming from the line where you instantiate tbxml you could try wrapping it in a
@try/@catchblock:Alternatively, download the XML data separately using NSURLConnection and use NSURLConnection’s delegate methods to handle error conditions. On success, pass the resulting data to a constructor such as
tbxmlWithXMLData:.EDIT: you’ve commented that the line causing the problem is:
The error you’re getting is because you’re trying to dereference a null pointer (
aParentXMLElementis nil, and calling->someMethodon it dereferences it). So all you need there is a guard clause to check aParentXMLElement is non-null before you attempt to dereference it. Something like this: