Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c Ensure that var-arg parameters are of the correct type and count

Tags:

objective-c

In objective-c how do you ensure that if you have a function that takes variable parameters that the format specifiers align with the actual parameters that are passed to the functoin?

like image 891
Lee Avatar asked Jun 07 '13 11:06

Lee


1 Answers

This is done through the use of the NS_FORMAT_FUNCTION macro.

Let's say that you have a function like this:

LOG(int level,NSString *format,...);

The level is the log level while the format contains the format string and the variable arguments are the parameters to the format string.

In order to ensure at compile time that the count and type of the parameters is correct one defines the function as this:

LOG(int level,NSString *format,...) NS_FORMAT_FUNCTION(2,3);

Note that the 2 and 3 here refer to the position in the arguments list of the format string and the start of the variable parameter list.

like image 62
Lee Avatar answered Sep 30 '22 09:09

Lee