Variadic function without zero completion

I am trying to create a method similar to the following:

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

But since I'm not very good at the preprocessor, I got into a problem that I fixed in the next code snippet, but I would like to know if there is a cleaner way to achieve what I want, which should stop after the arguments provided

 + (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: ([(=><)|(AZ)]{1,2})" userInfo:nil]; [exception raise]; } return [condition autorelease]; 

}

The problem is that the while loop is surrounded and walks past the provided arguments (I know why it gives me a different value, cmd args, etc.)

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

+1
source share
1 answer

The usual approach would be to first parse the format string and find out how many arguments should follow it based on this (usually there is only one valid number of arguments for any format string). If the number of arguments is not inferred from the format string, the list usually ends with zero (as in arrayWithObjects:... ).

+1
source

All Articles