Reputation: 1795
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!
Upvotes: 0
Views: 1753
Reputation: 20576
If the error's coming from the line where you instantiate tbxml you could try wrapping it in a @try/@catch
block:
TBXML *tbxml = nil;
@try {
tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://localhost/dev/mcomm/produto.xml"]] retain];;
}
@catch (NSException *exception) {
NSLog(@"Caught %@: %@", [exception name], [exception reason]);
}
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:
TBXMLElement * xmlElement = aParentXMLElement->firstChild;
The error you're getting is because you're trying to dereference a null pointer (aParentXMLElement
is nil, and calling ->someMethod
on 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:
if (aParentXMLElement != nil) {
TBXMLElement * xmlElement = aParentXMLElement->firstChild;
} else {
NSLog(@"Can't proceed: aParentXMLElement is nil");
}
Upvotes: 2