开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜