简体   繁体   English

检查某些东西是否为零的更好方法?

[英]Better way to check if something is nil?

I'm trying to parse a XML file using TBXML . 我正在尝试使用TBXML解析XML文件。 However, this parser has no error-checking built in so if an element doesn't exist it crashes. 但是,此解析器没有内置错误检查,因此如果元素不存在则崩溃。 Here's how I'm parsing one of my XML files: 这是我如何解析我的一个XML文件:

TBXML *XML = [[TBXML tbxmlWithXMLData:myxmlfile] retain];
if (XML.rootXMLElement) {
    TBXMLElement *XMLRoot = XML.rootXMLElement;
    if ([TBXML childElementNamed:@"blah" parentElement:XMLRoot]) {
        TBXMLElement *Blah = [TBXML childElementNamed:@"blah" parentElement:XMLRoot];   
        if ([TBXML childElementNamed:@"stuff" parentElement:Blah]) {
            TBXMLElement *Item = [TBXML childElementNamed:@"item" parentElement:Blah;
            if ([TBXML childElementNamed:@"stuff:blah" parentElement:Item]) {
                TBXMLElement *something = [TBXML childElementNamed:@"stuff:blah" parentElement:Item];
                NSString *Something = [TBXML textForElement:something];
                //do something here...
            }
            else {
               [self showFetchError];
                [XML release];
                return;}
        } else {
            [self showFetchError];
            [XML release];
            return;}
    } else {
        [self showFetchError];
        [XML release];
        return;}
} else {
    [self showFetchError];
    [XML release];
    return;
}

As you can see, it's making a call twice for each item. 如您所见,它正在为每个项目拨打两次电话。 That seems like a huge waste of overhead to me. 这对我来说似乎是一个巨大的浪费。 Any way I can do the same verification of each item without doing what I'm doing right now? 我可以用任何方式对每件物品进行相同的验证而不做我现在正在做的事情吗?

Here's a shorter version: 这是一个较短的版本:

BOOL success = NO;
TBXML *XML = [[TBXML tbxmlWithXMLData:myxmlfile] retain];
if (XML.rootXMLElement) {
    TBXMLElement *XMLRoot = XML.rootXMLElement;
    if ([TBXML childElementNamed:@"blah" parentElement:XMLRoot]) {
        TBXMLElement *Blah = [TBXML childElementNamed:@"blah" parentElement:XMLRoot];   
        if ([TBXML childElementNamed:@"stuff" parentElement:Blah]) {
            TBXMLElement *Item = [TBXML childElementNamed:@"item" parentElement:Blah;
            if ([TBXML childElementNamed:@"stuff:blah" parentElement:Item]) {
                success = YES;
                TBXMLElement *something = [TBXML childElementNamed:@"stuff:blah" parentElement:Item];
                NSString *Something = [TBXML textForElement:something];
                //do something here...
            }
        }
    }
}

if (!success) {
    [self showFetchError];
    [XML release];
    return;
}

Updated version using enumerator: 使用枚举器的更新版本:

TBXML *XML = [[TBXML tbxmlWithXMLData:myxmlfile] retain];
NSArray *path = [NSArray arrayWithObjects:@"blah", @"item", @"stuff:blah", nil];
NSEnumerator *e = [path objectEnumerator];
TBXMLElement *currentNode = XML.rootXMLElement;
BOOL success = NO;
while ((NSString *node = [e nextObject]) && currentNode) {
    if ([node isEqualToString:[path lastObject]]) {
        success = YES;
        NSString *Something = [TBXML textForElement:currentNode];
        // do the last element thing with string
    } else {
        currentNode = [TBXML childElementNamed:node parentElement:currentNode];
    }
}

if (!success) {
    [self showFetchError];
    [XML release];
    return;
}

I'm not sure if enumerator version works, but off top of my head it should solve your problem. 我不确定枚举器版本是否有效,但我不知道它应该解决你的问题。

You can do things like: 你可以这样做:

TBXMLElement *Blah = [TBXML childElementNamed:@"blah" parentElement:XMLRoot];
if (Blah) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM