/**
日誌打印, 多參數列表形式
__attribute__,是GNU編譯器的一個特性,這個宏是一個編譯器指令,我們在代碼中通過定義這個東西,可以提示 編譯器我們代碼的一些邏輯,從而在編譯器避免一些錯誤,在運行期提高性能。__attribute__在很多代碼中都有應用,非常實用。
__attribute__ format ,這個東西能告知編譯器,我們的代碼在處理printf,scanf這樣變參數的函數的時候,哪個參數是format string,哪個參數是參數列表,這樣可以避免代碼中的一些問題; 編譯的時候就能檢測
https://blog.csdn.net/houseq/article/details/38819901
@param format 格式化字符串
*/
+ (void)log: (NSString *)format, ... NS_FORMAT_FUNCTION(1, 2) {
// 1. 首先創建多參數列表
va_list args;
// 2. 開始初始化參數, start會從format中 依次提取參數, 類似於類結構體中的偏移量 offset 的 方式
va_start(args, format);
NSString *str = [[NSString alloc] initWithFormat:format arguments:args];
// 3. end 必須添加, 具體可參考 ?
// http://www.cppblog.com/ownwaterloo/archive/2009/04/21/is_va_end_necessary.html 此鏈接
va_end(args);
NSLog(@"%@",str);
}
/***
NS_FORMAT_FUNCTION(a,b) 函數的意義, ?源碼
// NSObjCRuntime.h
#if !defined(NS_FORMAT_FUNCTION)
#if (__GNUC__*10+__GNUC_MINOR__ >= 42) && (TARGET_OS_MAC || TARGET_OS_EMBEDDED)
#define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))
#else
#define NS_FORMAT_FUNCTION(F,A)
#endif
#endif
* 其中 a 代表 format 的位置, b 代表 可變參數的位置
*/
#define VALog(FORMAT, ...) [VaList log: FORMAT, ##__VA_ARGS__];
#import <Foundation/Foundation.h>
@interface VaList : NSObject
/**
多參數打印
@param format <#format description#>
*/
+ (void)log: (NSString *)format, ... NS_FORMAT_FUNCTION(1, 2) ;
@end