gpt4 book ai didi

ios - NSError 是否可以在 TBXML 中重用?

转载 作者:行者123 更新时间:2023-11-29 03:15:27 24 4
gpt4 key购买 nike

您好,我有一个解析器可以测试可能出现的错误,例如 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));
}

最佳答案

除非有错误(方法返回nil),否则不要检查error

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

关于ios - NSError 是否可以在 TBXML 中重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21742972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com