我可以将NSLog包装在一个带有可变数量参数的块中吗?

时间:2022-11-29 19:35:03

I’m writing an Objective-C library and I’d like it to offer a simple pluggable logging mechanism, so that the library user can turn the logging on and off. I thought an interesting way to do this would be a block property on the library classes:

我正在编写一个Objective-C库,我希望它提供一个简单的可插拔日志记录机制,以便库用户可以打开和关闭日志记录。我认为一个有趣的方法是在库类上使用块属性:

typedef void (^Logger)(NSString *fmt, ...);
@property(copy) Logger logger;
logger(@"Foo, %@.", self);

But I don’t know how to pass the variable argument list to NSLog:

但我不知道如何将变量参数列表传递给NSLog:

const Logger SimpleLogger = ^(NSString *fmt, ...) {
    // what goes in here?
};

1 个解决方案

#1


5  

Ah, I completely missed NSLogv:

啊,我完全错过了NSLogv:

const Logger SimpleLogger = ^(NSString *fmt, ...) {
    va_list arglist;
    va_start(arglist, fmt);
    NSLogv(fmt, arglist);
    va_end(arglist);
};

#1


5  

Ah, I completely missed NSLogv:

啊,我完全错过了NSLogv:

const Logger SimpleLogger = ^(NSString *fmt, ...) {
    va_list arglist;
    va_start(arglist, fmt);
    NSLogv(fmt, arglist);
    va_end(arglist);
};