Sonny G
Sonny G

Reputation: 1343

Is NSError reusable in TBXML?

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));
}

Upvotes: 0

Views: 72

Answers (1)

rmaddy
rmaddy

Reputation: 318884

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));
}

Upvotes: 1

Related Questions