Can I wrap NSLog in a block that takes a variable number of arguments?
I’m writing an Objective-C library and I’d like it to offer a simple pluggable logging mechanism, so that the library use开发者_如何学Pythonr can turn the logging on and off. I thought an interesting way to do this would be a block property on the library classes:
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
:
const Logger SimpleLogger = ^(NSString *fmt, ...) {
// what goes in here?
};
Ah, I completely missed NSLogv
:
const Logger SimpleLogger = ^(NSString *fmt, ...) {
va_list arglist;
va_start(arglist, fmt);
NSLogv(fmt, arglist);
va_end(arglist);
};
精彩评论