Objective-c确保var-arg参数的类型和计数正确 [英] Objective-c Ensure that var-arg parameters are of the correct type and count
问题描述
在Objective-C中,如何确保您的函数具有可变参数,且格式说明符与传递给functoin的实际参数对齐?
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?
推荐答案
这是通过使用NS_FORMAT_FUNCTION宏完成的.
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);
请注意,此处的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.
这篇关于Objective-c确保var-arg参数的类型和计数正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!