变量函数无nil终止 [英] Variadic function without nil termination

查看:161
本文介绍了变量函数无nil终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个类似下面的方法:

I am trying to create a method like the following:

- (void)setCondition:(NSString *)format,... NS_FORMAT_FUNCTION(1,2);

但是由于我对预处理器不是很好,我遇到了一个问题,代码片段,但我想知道是否没有更清洁的方式来实现我想要的是在提供的参数后停止

But since I'm not great with preprocessor, I hit an issue that I have fixed in the following code snippet, but I'd like to know if there's not cleaner way to achieve what I want which is to stop after the provided arguments

+ (CRCondition *)conditionWithFormat:(NSString *)format,... {
CRCondition *condition      = [[CRCondition alloc] init];

NSArray *conditionSliced    = [condition sliceFormatOperationFromString:format];

condition->_leftOperand     = [[conditionSliced objectAtIndex:0] retain];
condition->_operator        = [condition operatorFromString:[conditionSliced objectAtIndex:1]];
condition->_rightOperand    = [[conditionSliced objectAtIndex:2] retain];

id eachObject;
va_list argumentList;

va_start(argumentList, format);
while ((eachObject = va_arg(argumentList, id))) {
    if ([condition->_leftOperand isEqualToString:@"%K"]) {

        [condition->_leftOperand release];

        if ([eachObject isKindOfClass:[NSString class]])
            condition->_leftOperand = [eachObject retain];

        else
            condition->_leftOperand = [[eachObject description] retain];
    }

    else if ([condition->_rightOperand isKindOfClass:[NSString class]] &&
             [condition->_rightOperand isEqualToString:@"%@"]) {

        [condition->_rightOperand release];
        condition->_rightOperand = [eachObject retain];
    }

    else
        break;
}
va_end(argumentList);

if (![condition isOperatorValid]) {
    NSException *exception = [NSException exceptionWithName:@"Invalid Condition Operator" 
                                                     reason:@"The operator passed is invalid. Must follow the following regex pattern: ([(=><)|(A-Z)]{1,2})" 
                                                   userInfo:nil];
    [exception raise];
}

return [condition autorelease];

}

问题是while循环,通过提供的参数(我知道为什么它提供我其他的价值,cmd args等)

The problem is with the while loop that circles and go past the provided arguments (I'm aware of why it's providing me other value, cmd args and such)

如果你需要更多的解释,请

If you need any more explanation please add comments so I can get back to you.

推荐答案

通常的做法是先解析格式字符串,然后找出如何许多参数应该基于它(对于任何格式字符串通常只有一个有效数量的参数)。如果参数的数量不能从格式字符串中推导出来,通常使用nil(如 arrayWithObjects:... )终止列表。

The usual approach would be to parse the format string first and figure out how many arguments should follow it based on that (there is usually exactly one valid number of arguments for any format string). If the number of arguments is not deducible from a format string, it is common to terminate the list with nil (as in arrayWithObjects:...).

这篇关于变量函数无nil终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆