您好我有一个解析器可以测试可能的错误,例如D_TBXML_ELEMENT_TEXT_IS_NIL。如果obj1为nil且obj2有文本(NSError *),则错误仍返回非0值。除了重新分配0值之外,有没有办法清除以前的值?
我的代码是
- (void) parseOthers: (TBXMLElement *) element
{
do {
if (element -> firstChild)
[self parser:element->firstChild];
if ([[TBXML elementName:element] isEqualToString:@"myXML"]) {
MyClass *myClass = [[MyClass alloc] init];
NSError *error = nil;
// TBXML element obj1 has nil text.
myClass.myObject1 = [TBXML textForElement:[TBXML childElementNamed:@"obj1" parentElement:element] error:&error];
if(error){
NSLog(@"error in myObject1 > %@",[error localizedDescription]);
// Causes the second object to return a non nil error if I don't use the code below. error = nil;
error = nil;
}
// TBXML element obj2 has text but returns D_TBXML_ELEMENT_TEXT_IS_NIL if error = nil above isn't added.
myClass.myObject2 = [TBXML textForElement:[TBXML childElementNamed:@"obj2" parentElement:element] error:&error];
if(error){
NSLog(@"error in myObject2 > %@",[error localizedDescription]);
error = nil;
}
[myArray addObject:myClass];
}
} while ((element = element->nextSibling));
}
答案 0 :(得分:1)
除非出现错误(方法返回error
),否则请勿检查nil
。
- (void) parseOthers: (TBXMLElement *) element
{
do {
if (element -> firstChild)
[self parser:element->firstChild];
if ([[TBXML elementName:element] isEqualToString:@"myXML"]) {
MyClass *myClass = [[MyClass alloc] init];
NSError *error = nil;
// TBXML element obj1 has nil text.
myClass.myObject1 = [TBXML textForElement:[TBXML childElementNamed:@"obj1" parentElement:element] error:&error];
if(!myClass.myObject1){
NSLog(@"error in myObject1 > %@",[error localizedDescription]);
// Causes the second object to return a non nil error if I don't use the code below. error = nil;
error = nil;
}
// TBXML element obj2 has text but returns D_TBXML_ELEMENT_TEXT_IS_NIL if error = nil above isn't added.
myClass.myObject2 = [TBXML textForElement:[TBXML childElementNamed:@"obj2" parentElement:element] error:&error];
if(!myClass.myObject2){
NSLog(@"error in myObject2 > %@",[error localizedDescription]);
error = nil;
}
[myArray addObject:myClass];
}
} while ((element = element->nextSibling));
}