objective-c - xcode 中的 c 函数声明/用在调试和发布中表现不同的东西替换 NSLog

标签 objective-c c xcode

这是一个相当愚蠢的问题!但是在下面的函数中,你如何使用传入的剩余参数:

void NSLog(NSString *format, ...)
{
    //here I can use "format" but how can I use the remaining arguments?
}

很难找到这个问题的答案,因为我无法搜索“...”?! 顺便说一句,这就是 NSLog 的工作原理,但我把它放在这里只是为了举例,我的问题与 NSLog 无关。

最佳答案

使用可变参数列表:

void NSLog(NSString *format, ...)
{
    va_list ap;
    va_start(ap, format);

    while( (value = va_arg(args, NSString *) ){
        // Do something with value. This is assuming they are all strings
    }

    // or pass entire list to another function
    NSLog_VA( format, ap );

    va_end(ap);
}

void NSLog_VA( NSString * format, va_list args )
{
    // do something with va_list here
}

编辑: 由于您只需要调试日志:

#ifdef DEBUG 
#define DebugOnly_Log(format, args...) NSLog(format, ##args) 
#else 
#define DebugOnly_Log(format, args...) // defined to nothing
#endif 

关于objective-c - xcode 中的 c 函数声明/用在调试和发布中表现不同的东西替换 NSLog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6508956/

相关文章:

c - 如何在c语言中找到动态分配的大小?

ios - 如何创建一个可以采用类的值类型的空变量

ios - 暂停按钮在更改时添加新节点

c - 读取二进制文件并将所有数据存储到结构数组中

ios - 应用捏合手势时 UILabel 的文本模糊

c - 指向不在C中的列表中的项目

ios - 如何编译 libgit2 并创建 xcode 使用的静态库

objective-c - XCode 控制台中方括号中的数字是多少?

ios - 如何通过 objective-c 获取这样的 internet 请求信息

ios - 需要有关 ARC 概念的帮助