开发者

Can I wrap NSLog in a block that takes a variable number of arguments?

开发者 https://www.devze.com 2023-04-01 03:26 出处:网络
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 interestin

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);
};
0

精彩评论

暂无评论...
验证码 换一张
取 消