Reputation: 8068
I have a variadic function that runs some code on the first argument and then runs NSString
initWithFormat:arguments:
afterwards, if arguments have been passed in.
+ (NSString *)updatedString:(NSString *)mainString, ...
{
// Some code run on mainString here, outputs finalString
// add format arguments to the final string
va_list args;
va_start(args, mainString);
NSString *formattedString = [[NSString alloc] initWithFormat:finalString arguments:args];
va_end(args);
return formattedString;
}
EDIT: The idea is that the code run on mainString uses a regular expression to find and replace variables within the text. So say the output (finalString
) equals "Hello World %@ blah blah %@"
, then it would use the arguments to replace the %@
symbols. The issue is that I don't know if the resolved string includes %@
symbols or not until the string is resolved.
So, I need to check to see if there are actually any extra arguments, and if there aren't, I don't want to run the initiWithFormat
part.
Is there any way to check if args
exists first?
Upvotes: 0
Views: 221
Reputation: 122439
No. In C/Objective-C a called variadic function has absolutely no idea about the number and types of arguments passed by the caller. It must figure it out somehow based on other information (e.g. for format arguments, that they match the format specifiers in the format string; or for objects to initialize a Cocoa collection, that the list is terminated with nil
) and trust that the caller correctly followed the convention.
Upvotes: 3