0

Hi I have a parser which tests for possible errors such as D_TBXML_ELEMENT_TEXT_IS_NIL. If obj1 is nil and obj2 has text (NSError *) error still returns a non 0 value. Is there any way to clear the previous value other than re-assigning a 0 value?

My code is

- (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));
}
Sonny G
  • 1,331
  • 1
  • 12
  • 22
  • Only check the `error` parameter if the method call returns `nil`. – rmaddy Feb 13 '14 at 00:39
  • It seems that if I don't reset the error value to nil NSLog will have 2 "Element text is nil" even if the second xml element has text. – Sonny G Feb 13 '14 at 00:42
  • As I said, don't check the `error` unless the method returns `nil`. `error` is only modified if the method has an error. – rmaddy Feb 13 '14 at 00:46
  • If you want the `error` pointer to be cleared you need to clear it. – Hot Licks Feb 13 '14 at 01:57

1 Answers1

1

Don't check error unless there is an error (the method returns 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));
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Agreed about when to check `error`, but I'd recommend just using two different `NSError` variables here. In fact, if this is the real code, the OP should likely just refactor the shared code into another method (`[self textForKey:@"obj1"]`) that takes care of all the code duplication going on here (including the logging of an error). – Rob Napier Feb 13 '14 at 00:53
  • Okay tried this one but tbxml doesn't return an error even if the object text is nil. – Sonny G Feb 13 '14 at 02:54
  • Split the code into two lines. Store the result of `childElementNamed:` in a variable so you can check its result. Then call `textForElement:` with that variable. This makes the code easier to read and debug. – rmaddy Feb 13 '14 at 02:56