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.
source share